1

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 );
alfC
  • 14,261
  • 4
  • 67
  • 118

1 Answers1

1

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.

Justin
  • 24,288
  • 12
  • 92
  • 142
  • Odd name, given that the library is very functional. :) – alfC Jun 21 '19 at 00:08
  • 1
    @alfC Yeah. Many C++ libraries use the names from the C++ standard library, even if they are functional – Justin Jun 21 '19 at 00:09
  • It also seems to copy the elements instead of generating a sequence of references. That seems to be a big limitation of pop_back – alfC Jun 21 '19 at 00:24
  • Maybe if I do a `fusion::transform` I can manage to get rid of the temporary copy. `copy(pop_front(transform(a, [](auto& x)->decltype(auto){return x;})), b);` or even `copy( pop_front(transform(a, [](auto& x){return std::ref(x);})), b)`. – alfC Jun 21 '19 at 00:35
  • 1
    @alfC I don't believe it does copy. "Complexity: Constant. Returns a view which is lazily evaluated." – Justin Jun 21 '19 at 17:31
  • 1
    @alfC You can see that it doesn't do an extra copy: https://godbolt.org/z/ZglilB . However, because it returns a constant view, you can't move from it: https://godbolt.org/z/GbxHt5 – Justin Jun 21 '19 at 17:38
  • 1
    Sorry, didn't see the "view" remark in the documentation. It is a pity it doesn't do moves on views. I would use that because I am using this for a spiri x3 parser semantic action and in general the attributes should be moved into the targets (`val`s) because they are not used after the parsing. I think Fusion still needs some upgrading for C++11, in this case some kind of compile-time move-iterator/move-range. – alfC Jun 21 '19 at 18:52