C++ 宏表达式中的标签符号
Hashtag symbol in C++ macro expression
在下面的宏定义中,'#'符号是做什么的?这里的语法 (#x) 是什么?
#define print(x) cout<<(#x)<<" : "<<x<<endl;
#
是字符串化运算符。它将宏参数 x
转换为字符串文字。
我看不出有什么理由需要额外的括号,#define print(x) cout << #x <<" : " << x << endl;
也可以。 #define print(x) cout << #x <<" : " << (x) << endl;
更好,因为第二次使用 x
可能需要正确解析括号。
在下面的宏定义中,'#'符号是做什么的?这里的语法 (#x) 是什么?
#define print(x) cout<<(#x)<<" : "<<x<<endl;
#
是字符串化运算符。它将宏参数 x
转换为字符串文字。
我看不出有什么理由需要额外的括号,#define print(x) cout << #x <<" : " << x << endl;
也可以。 #define print(x) cout << #x <<" : " << (x) << endl;
更好,因为第二次使用 x
可能需要正确解析括号。