c++ - ## 宏参数连接没有像我预期的那样工作

标签 c++ macros concatenation c-preprocessor

不应该这样:

#define MOGSH_CONCAT (x,y) x##y
#define MOGSH_DOUBLE (a,b) MOGSH_CONCAT(a,b)
#define MOGSH_DEFINEPROC (p) MOGSH_DOUBLE(_gt,p) options_dialog::p;

MOGSH_DEFINEPROC(AssignMainForm);

愉快地扩展到:

_gtAssignMainForm options_dialog::AssignMainForm;

鉴于 _gt 未定义,_gtAssignMainForm 是:

typedef void (__stdcall *_gtAssignMainForm)();

options_dialog 只是一个类,其中 AssignMainForm 是一个静态成员。

相反,在 MSVC9 中,我收到错误:

'a' : undeclared identifier

在包含

的行上
MOGSH_DEFINEPROC(AssignMainForm);

最佳答案

在类函数宏的定义中,宏名称和参数列表开头的 ( 之间不能有空格。

#define MOGSH_CONCAT(x,y) x##y 
//                  ^ no whitespace allowed here

正如您现在所拥有的(带有空格),MOGSH_CONCAT 是一个类似对象的宏,带有 (x,y) x##y 的替换列表,它是为什么你会得到如此奇怪的结果。

关于c++ - ## 宏参数连接没有像我预期的那样工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5737533/

相关文章:

c++ - CHUNKSTATE 和 STAT_CHUNK 未定义

functional-programming - 请在 Scheme 中重构我的宏

c++ - C++ 错误代码样板的模板与宏

php - CakePHP 1.3 使用 CONCAT 多个字段查找条件接下来一小时的记录

python - 使用 pd.concat() 函数后出现问题 - 编码 latin-1 - 德语字符

c++ - 如何用 QTextEdit 结束一行

c++ - 'too few arguments to function ' char * strncpy(char *,const char *,size_t)'是什么意思?

c++ - 如何检查下标运算符是否存在?

vba - 优化 VBA/Excel 宏代码(在大工作表中查找重复项)

ruby-on-rails - 在 .erb 文件中使用 link_to,串联语法