Mat*_*son 10 c c++ c-preprocessor
在函数外面的cpp宏之后是否有任何强制分号的成语?
用于内部函数的宏的已知解决方案是:
#define MACRO(x) \
do {
x * 2;
} while(0)
Run Code Online (Sandbox Code Playgroud)
但是,假设我有一个如下所示的宏:
#define DETAIL(warning) _Pragma(#warning)
#define WARNING_DISABLE(warning) DETAIL(GCC diagnostic ignore warning)
Run Code Online (Sandbox Code Playgroud)
我可以在宏中强加一个在该语句后强制分号的内容.该语句可以在函数内部或外部使用:
WARNING_DISABLE("-Wunused-local-typedefs")
#include "boost/filesystem.hpp"
void foo(const int x) {
WARNING_DISABLE("-Wsome-warning")
...
}
Run Code Online (Sandbox Code Playgroud)
是否有任何C/C++语法会在文件中任何没有副作用的位置强制解析器中出现分号?
编辑:一个可能的用例:
#define MY_CPPUNIT_TEST_SUITE(test_suite_class) \
WARNING_PUSH \
/* the declaration of the copy assignment operator has been suppressed */ \
INTEL_WARNING_DISABLE(2268) \
/* the declaration of the copy assignment operator has been suppressed */ \
INTEL_WARNING_DISABLE(2270) \
/* the declaration of the copy constructor operator has been suppressed */ \
INTEL_WARNING_DISABLE(2273) \
CPPUNIT_TEST_SUITE(test_suite_class); \
WARNING_POP \
/* force a semi-colon */ \
UDP_KEYSTONE_DLL_LOCAL struct __udp_keystone_cppunit_test_suite ## __LINE__ {}
Run Code Online (Sandbox Code Playgroud)
小智 9
你不需要LINE技巧 - 它足以向前声明一些结构,允许多次,不需要实际的定义.与实际结构的冲突也不应成为问题.
#define DETAIL(warning) _Pragma(#warning) struct dummy
Run Code Online (Sandbox Code Playgroud)
#define DETAIL(warning) _Pragma(#warning) struct X ## __LINE__ {}
Run Code Online (Sandbox Code Playgroud)