开发者

Is there a way to make C macros keyword agnostic?

开发者 https://www.devze.com 2022-12-28 15:15 出处:网络
Is there a way to concatenate keywords in a macro and get C to behave in a more dynamic fashion as in:

Is there a way to concatenate keywords in a macro and get

C to behave in a more dynamic fashion as in:

#define macro(fun,ction,var,iable) function(variable)

I know this kind of thing exists in o开发者_开发问答ther languages.


You can use ## to concatinate names in macros

fun##ction ...


No. Although there is ## as Michael says, it is applied (as all preprocessing) before C or C++ looks at keywords, and even using it to generate any preprocessing keyword is allowed to crash the preprocessor.

So, as a rule, if something doesn't compile when you input it directly, it won't when generated by the preprocessor either.


There are some caveats to its use (e.g. you've got to jump through some hoops to concatenate the results of other macro expansions), but the GCC docs discuss the basic form:

http://gcc.gnu.org/onlinedocs/cpp/Concatenation.html


http://en.wikipedia.org/wiki/C_preprocessor#Token_concatenation

0

精彩评论

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