2014-07-25 28 views
7

以下面的例子,我想知道是否有替代boost::mpl::for_each,它沒有任何參數調用Functor。boost :: mpl :: for_each沒有實例化

#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()); 

好像for_each是默認實例化的類型。

回答

8

使用boost::typempl::_創建MPL拉姆達是轉換列表中的每個類型實例化的元素和調用功能,如在此之前:

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

Registrator應該是這個樣子:

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

希望有幫助。

+0

謝謝,像魅力:) – abergmeier

0

你應該使用三個參數的for_each:

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

那麼你得到的情況下,將是single_view

+0

代碼可悲的是不起作用。我也找不到任何示例如何正確使用'single_view'。 – abergmeier

+0

除此之外,您似乎只傳遞了部分類型的模板。 – abergmeier