[fusion] for_each with indices
I recently needed to iterate over a fusion sequence, calling a function for each element that depends not only on the element type but on its index (position within the sequence) as well. I didn't find anything readily available to use in conjunction with for_each, so I ended up using fold with the index as state. However, I would like to get the index as a compile time value. What would be the simpler/best way to do this? Creating an iterator adaptor that keeps track of advances to the base iterator (should I be using the iterator_facade extension mechanism)? Or perhaps zipping the sequence with some kind of infinite mpl::vector_c sequence? Agustín K-ballo Bergé.- http://fusionfenix.com
I had a similar problem - I needed to iterate over elements of a map and
find each element's index. It works by using template recursion, iterating
from the start of the sequence, with the exit specialization being when the
type of the current element being iterated over is the same as the type of
the element in the sequence you are working with.
A natural side effect of this is that the types in the sequence need to be
unique, otherwise later duplicate types will get their index to be the
index of the first element in the sequence with the same type.
Here is my code, it may help (I posted this on stackoverflow here:
http://stackoverflow.com/questions/11698413/find-the-index-of-an-element-in-...
)
#include <iostream>
#include
Agustín K-ballo Bergé
However, I would like to get the index as a compile time value. What would be the simpler/best way to do this? Creating an iterator adaptor that keeps track of advances to the base iterator (should I be using the iterator_facade extension mechanism)? Or perhaps zipping the sequence with some kind of infinite mpl::vector_c sequence?
I would use fusion::fold with the index stored as state via a mpl::int_. Christopher
On 30/07/2012 10:23 p.m., Christopher Schmidt wrote:
Agustín K-ballo Bergé
writes: However, I would like to get the index as a compile time value. What would be the simpler/best way to do this? Creating an iterator adaptor that keeps track of advances to the base iterator (should I be using the iterator_facade extension mechanism)? Or perhaps zipping the sequence with some kind of infinite mpl::vector_c sequence?
I would use fusion::fold with the index stored as state via a mpl::int_.
Simple enough! I hadn't considered the possibility of changing the State type from invocation to invocation. Thank you. Agustín K-ballo Bergé.- http://fusionfenix.com
participants (3)
-
Agustín K-ballo Bergé
-
Christopher Schmidt
-
Steve Lorimer