What's the meaning of “##” in a C++ macro? [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-12-02 04:28:51

问题


What's the meaning of "##" in the following?

#define CC_SYNTHESIZE(varType, varName, funName)\
protected: varType varName;\
public: inline varType get##funName(void) const { return varName; }\
public: inline void set##funName(varType var){ varName = var; }

回答1:


The operator ## concatenates two arguments leaving no blank spaces between them: e.g.

#define glue(a,b) a ## b
glue(c,out) << "test";

This would also be translated into:

cout << "test";



回答2:


It concatenates tokens without leaving blanks between them. Basically, if you didn't have the ## there

public: inline varType getfunName(void) const { return varName; }\

the precompiler would not replace funName with the parameter value. With ##, get and funName are separate tokens, which means the precompiler can replace funName and then concatenate the results.




回答3:


This is called token pasting or token concatenation.

The ## (double number sign) operator concatenates two tokens in a macro invocation (text and/or arguments) given in a macro definition.

Take a look here at the official GNU GCC compiler documentation for more information.



来源:https://stackoverflow.com/questions/18031280/whats-the-meaning-of-in-a-c-macro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!