我正在开发一个非常小的库,它将允许最终用户创建“命令”,可以通过在另一个函数中提供一个字符串来调用它call("this_function",params);
我做得很好,但是使用它的代码很难看:
#define begin(x) \
class x\
{\
x(){/*some code*/}\
void some_function_the_macro_has_to_make()
#define end() \
}
begin(hello_world)
{
/*do something*/
}
end();
我不知道如何在两个带有宏的片段之间插入代码,这甚至可能吗?
(而且我对如何在没有宏的情况下完成此操作知之甚少..)
所以我可以这样做:
#define begin(x){y} \
class x\
{\
x(){/*some code*/}\
void some_function_the_macro_has_to_make()\
{\
y\
}\
};
begin(hello_world)
{
/*do something*/
}
?
如果没有,是否有可能没有宏但有一些特殊的 C++ 东西?
编辑:
以下示例似乎有效,但在此问题的第二个代码示例中没有达到我想要的效果:
#define begin(x,y) \
class x\
{\
x(){/*some code*/}\
void some_function_the_macro_has_to_make()\
{\
y\
}\
};
begin(hello_world,
int x = 0;
std::cout << "x:" << x;
);
//it would be preffered to have:
begin(hello_world)
{
int x = 0;
std::cout << "x:" << x;
}