从boost mpl向量注册类型
Registering types from a boost mpl vector
std::string a= "a";
std::string b= "b";
std::string c= "c";
typedef mpl::vector<EasyFixEngineA,EasyFixEngineB,EasyFixEngineC> vecType;
RegisterInFactory<EasyFixEngine, mpl::at_c<vecType,0>::type> registerA( a);
RegisterInFactory<EasyFixEngine, mpl::at_c<vecType,1>::type,> registerB( b);
RegisterInFactory<EasyFixEngine, mpl::at_c<vecType,2>::type> registerC( c);
如何使用boost::mpl自动生成最后3行?我说的自动是指不需要重复3次"相同"行
使用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 {
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());
如果T
的实例化不适合您的情况,请参见此问题
简短的回答是你不能。
确实有三种不同的东西:-编译时执行(即。元编程),这是用MPL完成的。它们只对类型定义起作用-运行时执行(即。通常的编程),它是普通的c++(此时所有类型都已定义)-变量声明
你想使用MPL(编译时)来生成一些c++代码(变量声明)并做一些运行时(变量初始化)。
也许你想看看boost::fusion (fusion意味着编译时和运行时操作的结合)。如果你在boost::fusion中声明了三个变量,还有三个字符串,那么你可以使用boost::fusion::transform。但我不确定这是不是你想要的
相关文章:
- 使用QJsEngine在Qt中注册自定义类型
- 在Qt中注册自定义元类型的别名类型
- 如何注册枚举类型到QJSEngine,以便在脚本环境中使用?
- 在编译时生成某种子类/类型注册表?
- 将 const 类型引用对象注册为类成员对象C++
- 如何在QTScript中将QLIST内部的QMAP注册为元素类型
- 如何恢复注册表项的所有元素?(上次写入时间,类型,值,名称...这是正确的方法吗?
- UWP,加载窗口运行时组件 == 请求的窗口运行时类型 "..." 未注册
- 注册类型与容器
- 在多个DLL中注册常见的Qt元类型
- 读取 Qt 中的REG_MULTI_SZ类型注册表项
- 从C (例如Main.CPP)访问注册类型实例
- 在Qt中注册元类型的模式
- C++ 注册类型上的信号 QML 插槽
- 是否可以从对象而不是类注册 QML 类型
- 在 Angelscript 中为 glm::vec3 的基于 vic3 的值类型注册赋值运算符
- c++派生类型的自动工厂注册
- QWidget应用程序w/QML和注册类型
- 如何在编译时注册类型的键值对
- 从boost mpl向量注册类型