How to calculate nviews return type

I have the following problem:

template <int N, typename T> /*what is the return type*/ nviewgetter( T const& t ) { typename T::const_iterator it(t.begin()); typedef BOOST_TYPEOF_TPL(*it) etype; typedef typename boost::fusion::result_of::as_nview<etype, N>::type netype; std::vector<netype> r; while(it!=t.end()){ r.push_back( boost::fusion::as_nview<N>(*it) ); it++; } //return r; } 

T is expected to be a Forward Sequences sequence (for example, boost :: fusion :: vector), and I want to get an idea of ​​the Nth element in each element of T. However, I don’t know the type of boost::fusion::vector , for example . boost::fusion::vector<int, double> or boost::fusion::vector<int, double, std::string> . In the code, I can determine the correct type, but I cannot figure it out in the function declaration.

Thanks!

Any suggestions for improving the code are also welcome. :)

+4
source share
1 answer

If you do not want to write a full type, you can move the type definitions into a separate template so that they are available when you declare a function template; sort of:

 template <int N, typename T> struct nviewgetter_traits { typedef BOOST_TYPEOF_TPL(typename T::value_type) etype; typedef typename boost::fusion::result_of::as_nview<etype, N>::type netype; typedef std::vector<netype> result_type; // Or combine it into a single monstrosity if you prefer: // typedef std::vector< // typename boost::fusion::result_of::as_nview< // BOOST_TYPEOF_TPL(typename T::value_type), N // >::type> result_type; }; template <int N, typename T> typename nviewgetter_traits<N,T>::result_type nviewgetter(T const & t) { typename nviewgetter_traits<N,T>::result_type r; for (auto it = t.begin(); it != t.end(); ++it) { r.push_back( boost::fusion::as_nview<N>(*it) ); } return r; }; 
+4
source

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


All Articles