提高:: MPL :: for_each的没有实例 [英] boost::mpl::for_each without instantiating

查看:138
本文介绍了提高:: MPL :: for_each的没有实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下面的例子中,我不知道是否有的boost :: MPL :: for_each的,它不会调用一个函数对象没有任何参数的选择。

Taking the following example, I wonder whether there is an alternative to boost::mpl::for_each, which does call a 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());

看起来的for_each 的默认实例的类型。

推荐答案

使用的boost ::类型 MPL :: _ 以创建一个MPL的lambda的实例化的元素和调用功能,如在此之前转换列表中的每个类型:

Use boost::type and mpl::_ to create an MPL lambda that transforms each type in the list before instantiating the elements and calling the function, like this:

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

希望有所帮助。

这篇关于提高:: MPL :: for_each的没有实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆