The other day I was trying to use a macro to define a function like so (simplified, obviously):
#define DEF_ADD(name) \
int add_name(int x, int y) { \
return x + y; \
} \
Now, I expected the snippet DEF_ADD(hello)
to define the function add_hello
. However, the macro call instead defines a function called add_name
. I expect that this has something to do with scanning, but I couldn't find a way to recreate the behavior I'm looking for. Any ideas how to do this?
You need
#define DEF_ADD(name) \
int add_##name(int x, int y) { \
return x + y; \
}
Note well the ##
. I've also dropped the final newline character. Else the parameter name
is not used and you create add_name
as the function name.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.