1

I'm trying to iterate over a boost::fusion vector using:

typedef typename fusion::result_of::begin<T>::type t_iter;
  std::cout << distance(begin(t), end(t)) << std::endl;
  for(t_iter it = begin(t); it != end(t); next(it)){
    std::cout<<deref(it)<<std::endl;
  }

The distance cout statement gives me a finite length (2), however the loop seems to run indefinitely.

Any advice much appreciated!

arlogb
  • 681
  • 1
  • 9
  • 19

4 Answers4

4

You can't just iterate a Fusion vector like that, the type for each iterator may be different than the previous one (and usually is). I guess that's why you don't have it = next(it) in your code, it would give a compilation error.

You could use boost::fusion::for_each for this, together with a function object that prints each element to the standard output:

struct print
{
    template< typename T >
    void operator()( T& v ) const
    {
        std::cout << v;
    }
};

...

boost::fusion::for_each( t, print() );
g-217
  • 2,069
  • 18
  • 33
K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • Thanks! Actually I want to assign each element of the vector a value. I assume I should do something similar? – arlogb Oct 26 '12 at 21:30
  • @arlogb: While you could do something similar, it's probably easier to just generate a new compatible sequence with the new values and assign it to the target sequence. – K-ballo Oct 26 '12 at 21:32
  • thanks for the advice. Unfortunately I don't know how many values I want to assign till runtime, so whereas I'd use something like `push_back` on a `std::vector` here I'm still a little lost as to how to assign values. – arlogb Oct 26 '12 at 22:14
  • @arlogb: _Fusion Sequences_ are typed beasts, you can't have a dynamic sequence. That is, the result of `push_back` on a _Sequence_ results in a **new** _Sequence_. – K-ballo Oct 26 '12 at 22:29
  • OK, maybe I haven't phrased my problem very well. Essentially I'm expecting to get something like `fusion::vector` or `fusion::vector` passed to a (templated) function and would like to fill whatever I get with some values. – arlogb Oct 26 '12 at 22:36
2

fusion is a wonderful library, and you should now that it is really different from what you use in every day C++ programs in multiple ways, it merge the power of compile time meta programming with runtime, for that you should now that there is no type that can handle all items in a fusion container. What this means? it means that result_of::begin<T>::type is not always a match of next(it) so you can't use fusion iterators in a for like that.

The obvious problem in your code is that you ignore return value of next and it will cause your code to run forever but you can't use it in it = next(it), since their type may vary!!

So what you should do?? You should use boost::fusion::for_each for that purpose

BigBoss
  • 6,904
  • 2
  • 23
  • 38
1

next doesn't actually advance the iterator, it just returns the next one.

This can be seen in the docs, as the function next takes a constant argument, meaning it can't possibly actually modify the iterator:

template<
    typename I
    >
typename result_of::next<I>::type next(I const& i);
                                         ^^^^^
chris
  • 60,560
  • 13
  • 143
  • 205
  • Ah yes, you're quite right. Is there a way to advance the iterator then? I see the `advance` method also takes a `const` so I assume that doesn't move the iterator either. – arlogb Oct 26 '12 at 21:24
  • 1
    @arlogb, Advancing the same iterator gives you the problem discussed in K-Ballo's answer. I would suggest the general solution boost provides for iterating the entire vector that is `for_each`, which is nicely demonstrated for you in the very same answer. – chris Oct 26 '12 at 21:27
0

The problem is that inside the loop you are dereferencing your iterator. When you apply next on it, it means nothing and that's why your loop runs forever.

alestanis
  • 21,519
  • 4
  • 48
  • 67