【问题标题】:boost::mpl::for_each without instantiatingboost::mpl::for_each 没有实例化
【发布时间】:2014-07-25 10:59:49
【问题描述】:

以下面的例子,我想知道是否有替代boost::mpl::for_each,它确实调用了一个没有任何参数的Functor。

#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 是默认实例化类型。

【问题讨论】:

    标签: c++ boost boost-mpl


    【解决方案1】:

    使用boost::typempl::_ 创建一个MPL lambda,在实例化元素和调用函数之前转换列表中的每种类型,如下所示:

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

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      你应该使用三个参数for_each:

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

      那么你得到的实例将是single_view

      【讨论】:

      • 遗憾的是,该代码不起作用。我也找不到任何如何正确使用single_view的示例。
      • 此外,您似乎只传递了部分类型的模板。
      猜你喜欢
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多