简体   繁体   中英

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:

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.

String literals can be concatenated, constant pointers to constant strings can't. That's the difference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM