4

I'm painfully trying to learn boost fusion and I dont understand clearly the differences between zip_view and the result of zip function.

namespace fuz = boost::fusion;

typedef fuz::vector<int,int> vec1;
typedef fuz::vector<char,char> vec2;
typedef fuz::vector<vec1&, vec2&> sequences;

typedef fuz::zip_view<sequences> zip_view_type;

typedef fuz::result_of::zip<vec1, vec2>::type zip_result_type;

BOOST_MPL_ASSERT((boost::is_same<zip_view_type, zip_result_type>));
  • I expected the two types to be the same, but they are not. Why?

  • zip_view and zip function seems to be very closely related but I dont see when / why using the view instead of the function.

Laurent
  • 812
  • 5
  • 15
  • 1
    According to the documentation of `zip_view`, `sequences` should be `fuz::vector`. `result_of::zip` seems to use `vector2` instead of `vector` internally, so if you use `vector2` in `vec1`, `vec2` and `sequences` the types seem to be the same ([live example](http://coliru.stacked-crooked.com/a/e2bc2f794e33c64c)). Hopefully someone who knows more will give you a better answer. – llonesmiz Nov 25 '13 at 17:12
  • @cv_and_he : thx, at least your live example demonstrates the diff. I tried to use type_info too but with g++ (even with name demangling) the output was too cryptic. I will also correct the sequences typedef in my question. – Laurent Nov 25 '13 at 17:19
  • 1
    Ok `vector2` is just the numbered form of 2 elements vector. `vector` is the variadic form. What I understand here is that `result_of::zip::type` will use the numbered form of vector and zip_view will use the type of the given sequence, thus here : the variadic form of vector( `vector`). – Laurent Nov 26 '13 at 16:56

1 Answers1

3

I hope I can answer your second question (why using zip_view instead of zip).

The matter is that zip produces a sequence of tuples containing constant references to respective elements of zipped sequences. In your example it is vector2<const int&, const char&>

In contrast, references in tuples produced by zip_view for each element have the same const qualifier as zipped sequence in the view constructor. It will be vector<int&, char&> in your case.

As a result, zip_view allows something that zip does not support:

  1. Modification of elements of zipped sequences;
  2. Selection of sequences to be modified.
Dmitry
  • 31
  • 1