Fru*_*ose 5 c c++ stringification c-preprocessor
我遇到了一个断言的例子,并想知道它#是为了什么:
#define ASSERT( x ) if ( !( x ) ) { \
int *p = NULL; \
DBGPRINTF("Assert failed: [%s]\r\n Halting.", #x); \
*p=1; \
}
Run Code Online (Sandbox Code Playgroud)
它是"stringize"预处理运算符.
它将作为参数传递的标记作为宏参数x并将它们转换为字符串文字.
#define ASSERT(x) #x
ASSERT(a b c d)
// is replaced by
"a b c d"
Run Code Online (Sandbox Code Playgroud)
#x是字符串化指令
#define Stringify(x) #x
手段 Stringify(abc)将被替换为"abc"
如
#define initVarWithItsOwnName(x) const char* p = #x
int main()
{
initVarWithItsOwnName(Var);
std::cout << Var; //will print Var
}
Run Code Online (Sandbox Code Playgroud)