开发者

Variable name from macro argument

开发者 https://www.devze.com 2023-04-07 13:25 出处:网络
I\'d like to do something like this: class SomeClass { }; GENERATE_FUNTION(SomeClass) The GENERATE_FUNCTION macro I\'d like to define a function whose name is to be determined by the macro argumen

I'd like to do something like this:

class SomeClass { };

GENERATE_FUNTION(SomeClass)

The GENERATE_FUNCTION macro I'd like to define a function whose name is to be determined by the macro argument. In t开发者_如何学Gohis case, I'd like it to define a function func_SomeClass. How can that be done?


#define GENERATE_FUNCTION(Argument) void func_##Argument(){ ... }

More information here: http://en.wikipedia.org/wiki/C_preprocessor#Token_concatenation


As everyone says, you can use token pasting to build the name in your macro, by placing ## where needed to join tokens together.

If the preprocessor supports variadic macros, you can include the return type and parameter list too:

#define GENERATE_FUNCTION(RET,NAM,...) RET func_##NAM(__VA_ARGS__)

..so, for example:

GENERATE_FUNCTION(int,SomeClass,int val)

..would expand to:

int func_SomeClass(int val)


#define GENERATE_FUNCTION(class_name) func_##class_name##
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号