Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6822697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:41:41+00:00 2026-05-26T21:41:41+00:00

I have a boost::mpl::vector with N elements, say: typedef boost::mpl::vector<int,float,double,short,char> my_vector; I wish to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:41:41+00:00Added an answer on May 26, 2026 at 9:41 pm

    Erase is a reasonable solution for your problem.

    • The value you want for first is the result of mpl::begin<T> which is advanced by the number of elements you are interested in returning.
    • The value you want for end is the result of 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_elements which takes an MPL integral constant and first_n_elements_c which 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.

    #include <boost/mpl/vector.hpp>
    #include <boost/mpl/size.hpp>
    #include <boost/mpl/erase.hpp>
    #include <boost/mpl/eval_if.hpp>
    #include <boost/mpl/int.hpp>
    #include <boost/mpl/less.hpp>
    namespace mpl = boost::mpl;
    
    
    
    namespace detail 
    {
      // Note, this is an internal detail.  Please use the structures below
      template <typename T, typename N>
      struct erase_after_n
      {
        typedef typename mpl::begin<T>::type begin_iter;
        typedef typename mpl::advance<begin_iter, N>::type cut_iter;
        typedef typename mpl::end<T>::type end_iter;
    
        typedef 
        typename mpl::erase< T,cut_iter, end_iter >::type type;
    
      };
    
    }
    
    
    template <typename T, typename N> 
    struct first_n_elements
    {
      typedef 
      typename mpl::eval_if< mpl::less < mpl::size<T>, N >,
                 T,
                 detail::erase_after_n<T, N> >::type type;
    
    };
    
    template <typename T, int N> 
    struct first_n_elements_c
    {
      typedef 
      typename first_n_elements<T, mpl::int_<N> >::type type ;
    
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a boost variant of looking like this: typedef boost::variant<int, float, double, long,
I have a list of types defined as: typedef boost::mpl::list<Apple, Pear, Brick> OriginalList; I
I have the following code and it works fine. #include <boost\mpl\vector.hpp> #include <boost\mpl\fold.hpp> #include
Let's say I have a boost::mpl::list< A, B, C ...> . How do I
I'm writing a template function which should swap two elements of a boost::mpl::vector (similarly
Is there an existing function (in boost mpl or fusion) to splat meta-vector to
I'm new to the Boost.MPL library, and have some beginners-problems Look at this sample:
Consider the following code that automates generation of Boost.MPL type sequences (list or vector).
I have a class that accepts a list of policy classes using boost::mpl. Each
In file A.hpp, I have extern boost::signal<void (model::Bullet&, Point&, Point&, int)> signal_createBullet; and so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.