5
我想建立一個模板函數來將std :: array轉換爲一個具有接受其座標參數的構造函數的普通點。我的問題是:有沒有辦法擴大數組a
?如何將std :: array轉換爲點?
我想建立一個模板函數來將std :: array轉換爲一個具有接受其座標參數的構造函數的普通點。我的問題是:有沒有辦法擴大數組a
?如何將std :: array轉換爲點?
template <typename PointT, std::size_t N, std::size_t... Is>
PointT to(std::array<double, N> const& a, std::index_sequence<Is...>)
{
return PointT(a[Is]...);
}
template <typename PointT, std::size_t N>
PointT to(std::array<double, N> const& a)
{
return to<PointT>(a, std::make_index_sequence<N>{});
}
注:index_sequence
/integer_sequence
工具可以從C++ 14開始。由於這個問題被標記爲C++11
,從這個答案演示代碼利用下面的實現:
namespace std
{
template <std::size_t... Is>
struct index_sequence {};
template <std::size_t N, std::size_t... Is>
struct make_index_sequence_h : make_index_sequence_h<N - 1, N - 1, Is...> {};
template <std::size_t... Is>
struct make_index_sequence_h<0, Is...>
{
using type = index_sequence<Is...>;
};
template <std::size_t N>
using make_index_sequence = typename make_index_sequence_h<N>::type;
}
該演示通過在'std'中引入新符號來做未定義的行爲;) – Yakk 2014-10-18 17:43:30
*「我的問題是:有沒有辦法擴大數組?」 *是的,但它是醜陋的。你不能將一對迭代器傳遞給'PointT'構造函數嗎? – dyp 2014-10-18 17:25:28