0

For my current project it is necessary to iterate over a boost::fusion container. For this I found boost::fusion::for_each quite useful. Now it is necessary for me to increase a counter for each time the operator() of Functor got called.

Firstly, I tried to add a member int count to Functor, but this is not possible, as it is necessary that operator() must be a const Memberfunction of Functor.

The following short code snippet is an illustration of what I wanted to achieve.

#include <boost/fusion/container/vector.hpp>
#include <string>
#include <boost/fusion/include/for_each.hpp>
#include <iostream>

struct Functor {
    template<typename T>
    void operator()(T& t) const{
        std::cout << typeid(t).name() << " -> " << t << std::endl;
        // Desired Output:
        // int -> 5 : 0
        // std::string -> test : 1
        // double -> 5.3 : 2
        // double -> 10. : 3
    }
};

int main(int argc, char**args) {
    typedef boost::fusion::vector<int, std::string, double, double> TData;
    TData v(5, std::string("test"), 5.3, 10.);
    boost::fusion::for_each(v, Functor());
}

Is there a workaround for this kind of problem?

Aleph0
  • 5,816
  • 4
  • 29
  • 80

0 Answers0