Boost :: mpl :: for_each without instantiating

In the following example, I wonder if there is an alternative to boost::mpl::for_each that calls Functor without any arguments.

 #include <boost/mpl/vector.hpp> #include <boost/mpl/for_each.hpp> struct EasyFixEngineA { static const char* const name() { return "a"; } }; struct EasyFixEngineB { static const char* const name() { return "b"; } }; struct Registrator { // Would prefer a template<class T> void operator()() template<class T> void operator()(T t) { RegisterInFactory<EasyFixEngine, T> dummy(T::name()); } }; // ... typedef boost::mpl::vector<EasyFixEngineA,EasyFixEngineB> Engines; boost::mpl::for_each<Engines>(Registrator()); 

It seems that for_each used to create default instance types.

+7
c ++ boost boost-mpl
source share
2 answers

Use boost::type and mpl::_ to create a lambda MPL that converts each type to a list before creating the elements and calling the function, for example:

 mpl::for_each<Engines, boost::type<mpl::_> >(Registrator()); 

Registrator should look something like this:

 struct Registrator { template<typename T> void operator()(boost::type<T>) const { RegisterInFactory<EasyFixEngine, T> dummy(T::name()); } }; 

Hope this helps.

+8
source share

You should use three for_each parameters:

 mpl::for_each<Engines,mpl::single_view>(Registrator()) 

Then the instances you get will be single_view .

0
source share

All Articles