How to convert std :: array to a point?

I want to build a template function to convert std :: array to a common point that has a constructor that takes its coordinate arguments.

template<typename PointT, size_t N> PointT to(std::array<double, N> const& a) { return PointT(a[0], a[1], ...); // How to expand a? } 

My question is: is there a way to expand the array a ?

+5
source share
1 answer
 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>{}); } 

Demo


Note : index_sequence / integer_sequence Utilities are available starting with C ++ 14. Since the question is marked as C++11 , the demo code from this answer uses the following implementation:

 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; } 
+7
source

Source: https://habr.com/ru/post/1204984/


All Articles