What is the meaning of the # sign in this C++ macro? -


what # mean in c++ macro:

#define assert(expr)   \    {if (!(expr)){cm_error_trace("assert  "<< #expr <<" failed"); assert(expr);} } 

this "stringizing" operator. in macro, if x macro parameter, #x evaluates quoted string literal representation of text of x. example, macro

#define stringize(x) #x 

evaluated in context

stringize(2 + 3 + 4) 

expands out to

"2 + 3 + 4" 

here, macro is

#define assert(expr)   \    {if (!(expr)){cm_error_trace("assert  "<< #expr <<" failed"); assert(expr);} } 

the use of #expr here means if expression not evaluate true, print out string representation of macro argument. example

assert(myfunction()) 

would expand to

{if (!(myfunction())){cm_error_trace("assert " << "myfunction()" << " failed"); assert(myfunction());} } 

hope helps!


Comments