Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the C++ preprocessor tell whether a token is a string?

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

For example:

#define IS_STRING(token) ???

IS_STRING("foo")  // expands to 1
IS_STRING(foo)    // expands to 0
like image 519
HighCommander4 Avatar asked Oct 07 '22 03:10

HighCommander4


1 Answers

Yes. But with a small difference in the output:

#define IS_STRING(token) "" token 

It will go fine for string literal. For non-strings, it will give compiler error.

Logic: Compiler concatenates string literal automatically, so "" token goes fine, if token is a string literal.

Here is a related discussion.

like image 156
iammilind Avatar answered Oct 13 '22 11:10

iammilind