C++ 将两个宏合并为一个宏/创建在两个静态代码段之间插入代码的宏?
我正在开发一个非常小的库,它将允许最终用户创建“命令”,可以通过在另一个函数中提供字符串来调用该命令,如C++ 将两个宏合并为一个宏/创建在两个静态代码段之间插入代码的宏?,c++,macros,C++,Macros,我正在开发一个非常小的库,它将允许最终用户创建“命令”,可以通过在另一个函数中提供字符串来调用该命令,如call(“this_函数”,params) 我做得很好,但使用它的代码很难看: #define begin(x) \ class x\ {\ x(){/*some code*/}\ void some_function_the_macro_has_to_make() #define end() \ } begin(hello_wor
call(“this_函数”,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;
}
#定义开始(x,y)\
x类\
{\
x(){/*一些代码*/}\
使某些函数无效\u宏\u要\u生成()\
{\
y\
}\
};
开始(你好,世界,
int x=0;
std::cout我不确定我是否完全理解您的目标,但我认为您希望避免使用end()
宏
如果是这种情况,您可以更改宏以在类
声明之外定义方法
#define begin(x) \
class x\
{\
x(){/*some code*/}\
void some_function_the_macro_has_to_make(); \
}; \
inline void x::some_function_the_macro_has_to_make ()
现在,您可以这样使用它:
begin(hello_world) {
int x = 0;
std::cout << "x:" << x;
}
begin(你好,世界){
int x=0;
std::cout我不确定我是否完全理解您的目标,但我认为您希望避免使用end()
宏
如果是这种情况,您可以更改宏以在类
声明之外定义方法
#define begin(x) \
class x\
{\
x(){/*some code*/}\
void some_function_the_macro_has_to_make(); \
}; \
inline void x::some_function_the_macro_has_to_make ()
现在,您可以这样使用它:
begin(hello_world) {
int x = 0;
std::cout << "x:" << x;
}
begin(你好,世界){
int x=0;
std::cout关于#define begin(x,y)
?如果你把代码放在,
之后,这似乎是可行的,但是有可能让它像第二个例子那样运行吗?(我想要实现的)#define CMD(x)y begin(x,y)CMD(second){int x=0;}
似乎没有编译(甚至没有提到程序员需要)
在结尾,这看起来更难看)那么定义begin(x,y)
如何呢?如果你把代码放在之后,
这似乎是可行的,但是有可能使它的行为像第二个例子吗?(我想要实现的)定义CMD(x)y begin(x,y)CMD(second){int x=0;}
似乎没有编译(甚至不提程序员需要)
在结尾,这看起来更难看)是的,是的,我的英雄关于函子模板的东西,你能举个例子吗?这是我写的迷你库:如果这有助于制作示例..是的,我的英雄关于函子模板的东西,你能举个例子吗?这是我写的迷你库:如果这有助于制作示例。。