Can a C ++ preprocessor determine if a token is a string?

Is it possible for a preprocessor macro to determine if its argument is a string (literal) or not?

For instance:

#define IS_STRING(token) ???

IS_STRING("foo")  // expands to 1
IS_STRING(foo)    // expands to 0
+5
source share
1 answer

Yes. But with a slight difference in output:

#define IS_STRING(token) "" token 

This will be good for a string literal. For non-strings, this will give a compiler error.

Logic : the compiler automatically concatenates a string literal, so it "" tokengoes fine if it tokenis a string literal.

Here is a related discussion .

+4
source

All Articles