Why this code does not compile:
#include <boost/mpl/vector.hpp>
#include <boost/mpl/for_each.hpp>
#include <iostream>
using namespace std;
using namespace boost;
template <class T> // specific visitor for type printing
static void print_type(T t)
{
std::cout << typeid(T).name() << std::endl;
}
typedef mpl::vector<int, long, char*> s;
int main ()
{
mpl::for_each<s>(print_type());
}
I wonder – how to make boost mpl for_each work with free functions from same class?
As stated you need a functor.
The code below includes an additional wrap template that allows the print functor to cope with references.
Note: this code based on examples in the book ‘C++ Template Metaprogramming by David Abrahams and Aleksy Gurtovoy