简体   繁体   English

宏内的宏扩展

[英]Macro expansion within a macro

I'm trying to create LOGDEBUG macro: 我正在尝试创建LOGDEBUG宏:

#ifdef DEBUG
#define DEBUG_TEST 1
#else
#define DEBUG_TEST 0
#endif

#define LOGDEBUG(...) do { if (DEBUG_TEST) syslog(LOG_MAKEPRI(LOG_SYSLOG, LOG_DEBUG),  __VA_ARGS__); } while (0)

...

size_t haystack_len = fminl(max_haystack_len, strlen(haystack_start));
LOGDEBUG(("haystack_len %ld\n", haystack_len));

I am not using # or ## parameters to stringify the arguments, and yet g++ apparently tries to stringify them: 我没有使用#或##参数来对参数进行字符串化,但是g ++显然试图对它们进行字符串化:

numexpr/interpreter.cpp:534:5: error: invalid conversion from ‘size_t {aka long unsigned int}’ to ‘const char*’ [-fpermissive]

Note that haystack_len is size_t and I do not convert it to char* in the macro, yet compiler sees it as such. 注意, haystack_lensize_t ,我没有在宏中将其转换为char* ,但是编译器认为是这样。 Does g++ implicitly tries to convert macro arguments to strings? g ++是否隐式尝试将宏参数转换为字符串?

How to fix that? 如何解决? I mean, I'm using gnu LOG_MAKEPRI macro for syslogging, is it this macro that may be causing trouble? 我的意思是,我正在使用gnu LOG_MAKEPRI宏进行系统日志记录,这可能是引起麻烦的宏吗? Also, is there some way to see the macro-expanded code? 另外,有什么办法可以看到宏扩展代码?

How to fix that? 如何解决?

LOGDEBUG(("haystack_len %ld\\n", haystack_len)); call the macro with one unique argument. 用一个唯一的参数调用宏。 So it will produce: 因此它将产生:

do { if (DEBUG_TEST) syslog(LOG_MAKEPRI(LOG_SYSLOG, LOG_DEBUG), ("haystack_len %ld\n", haystack_len)); } while (0);

And ("haystack_len %ld\\n", haystack_len) use comma operator and result in haystack_len 并且("haystack_len %ld\\n", haystack_len)使用逗号运算符并导致haystack_len

So you have to call it that way: LOGDEBUG("haystack_len %ld\\n", haystack_len); 因此,您必须这样称呼: LOGDEBUG("haystack_len %ld\\n", haystack_len);

Also, is there some way to see the macro-expanded code? 另外,有什么办法可以看到宏扩展代码?

gcc -E may help. gcc -E可能有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM