[英]Compile time concatenation of computed string literals
We can concatenate adjacent string literals like so: 我们可以连接相邻的字符串文字,如下所示:
puts( "ABC" "DEF" );
However, MSVC fails with a strange error when I try to do this: 但是,当我尝试执行此操作时,MSVC失败并出现奇怪错误:
puts( ("ABC") ("DEF") );
Which means I can do a single computation outputting a string literal like so: 这意味着我可以执行单个计算输出字符串文字,如下所示:
puts( NUM_ELEMENTS>125?"WARNING":"OK" )
But I can't concatenate the string literals output from multiple of these, such as: 但我无法连接多个这些字符串文字输出,例如:
#define SOME_SETTING 0x0B //I sometimes wish there were binary literals
#define BIT_STR(x,n) ((x>>n)&1?"1":"0")
#define BIT_STR4(x) BIT_STR(x,3) BIT_STR(x,2) BIT_STR(x,1) BIT_STR(x,0)
...
puts( "Initializing some hardware setting: " BIT_STR4(SOME_SETTING) );
EDIT: So my question is... what is the correct way to concatenate compile time computed string literals? 编辑:所以我的问题是...什么是连接编译时计算字符串文字的正确方法?
BIT_STR(SOME_SETTING, 3)
, to take an example, can indeed be computed on runtime: it results to (0?"1":"0")
, which in turn results to a pointer to a constant string "0"
, not to a string literal any longer. BIT_STR(SOME_SETTING, 3)
,举一个例子,确实可以在运行时计算:它导致(0?"1":"0")
,这反过来导致指向常量字符串"0"
的指针,而不是再到字符串文字。
String literals can be concatenated, constant pointers to constant strings can't. 字符串文字可以连接,常量字符串的常量指针不能。 That's the difference.
这就是区别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.