SlowProgrammer
SlowProgrammer

Reputation: 1

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?

Upvotes: 0

Views: 364

Answers (1)

glglgl
glglgl

Reputation: 91119

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.

Upvotes: 2

Related Questions