gpt4 book ai didi

c++ - boost::mpl::for_each 没有实例化

转载 作者:可可西里 更新时间:2023-11-01 16:36:53 25 4
gpt4 key购买 nike

以下面的例子为例,我想知道是否有 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 是默认实例化类型。

最佳答案

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

希望对您有所帮助。

关于c++ - boost::mpl::for_each 没有实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24954220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com