I have a boost::mpl::vector with N elements, say:
typedef boost::mpl::vector<int,float,double,short,char> my_vector;
I wish to obtain a sequence containing the first M elements of my_vector. So if M is 2 I want out a:
typedef boost::mpl::vector<int,float> my_mvector;
Initially I thought of using erase<s,first,last> but was unable to figure out suitable template parameters for first and last. (I was using at_c<...>::type.) However, it is also my understanding that filter_view can also be used for the task. What is the best way of going about this?
Erase is a reasonable solution for your problem.
mpl::begin<T>which is advanced by the number of elements you are interested in returning.mpl::end<T>The code below assumes that you want the metafunction to return the original type if the number of elements in the vector is less than the requested number. It’s also possible to use a static assertion to verify that input integral type is less than or equal to the size of the vector.
I provied both a
first_n_elementswhich takes an MPL integral constant andfirst_n_elements_cwhich simply takes an integer.You could also use
iterator_range<>along with the begin and cut iterators in the below code, if you want to use a view. I’m not sure what the advantages are of one over the other in this case.