c++c++11boost-fusion

Does Fusion have a tail function?


I need a tail-like funciton that can be used like this:

boost::fusion::vector<char, int, float> a('a', 12, 5.5f);
boost::fusion::vector<int, float> b(12, 5.5f);

boost::fusion::copy( Tail(a), b );

Solution

  • In the documentation for Boost Fusion, there's a section under Algorithms called Transformation. The Functions listed here notably include one called pop_front. This seems to do exactly what we want:

    Returns a new sequence, with the first element of the original removed.
    ...

    Example

    assert(pop_front(make_vector(1,2,3)) == make_vector(2,3));
    

    For your example:

    boost::fusion::vector<char, int, float> a('a', 12, 5.5f);
    boost::fusion::vector<int, float> b(12, 5.5f);
    
    boost::fusion::copy( boost::fusion::pop_front(a), b );
    

    The name pop_front is a little strange, considering that it doesn't actually modify the input sequence, but returns a modified result. However, pop_front comes from the C++ standard library, where it is used for removing the first element of a collection such as with std::list::pop_front. Boost Fusion chose this name to be "more consistent" with the standard library.