我的代码中有这些行:
//lines in mycode.c++
QString str = "...some id...";
if( str == "int")
foo< int>()
else if( str == "QString")
foo< QString>()
...
我需要创建一种机制以在此条件语句中包含自定义类型。因此,任何程序员都可以注册他的类和foo模板函数的实现。
我想象它是这样的:
//A.h -- custom class
class A { };
template< >
void foo< A>() { ... };
DECL( A, "A"); //macro to declare class
我想要mycode.c++中的条件语句,它会自动考虑类A 的声明,所以它会有额外的行:
else if( str == "A")
foo< A>()
我可以有这样的效果:
//common.h
void process_id( QString str) {
if( str == "int")
foo< int>()
else if( str == "QString")
foo< QString>()
...
else if( str == "A") //this lines programmer put manually
foo< A>();
}
//mycode.c++
#include "common.h"
QString str = "some_id";
process_id( str);
但是如果程序员忘记编辑common.h文件怎么办?
我想,也许是使用 C 宏系统,或者以某种方式使用 Qt 预编译。可能吗?