Is there any idiom that forces a semicolon after a cpp macro outside of a function?
The known solution for macros used inside functions is:
#define MACRO(x) \
do {
x * 2;
} while(0)
However, say I have a macro that looks like the following:
#define DETAIL(warning) _Pragma(#warning)
#define WARNING_DISABLE(warning) DETAIL(GCC diagnostic ignore warning)
What can I put in the macro that would force a semi-colon after that statement. The statement could be used in or outside of a function:
WARNING_DISABLE("-Wunused-local-typedefs")
#include "boost/filesystem.hpp"
void foo(const int x) {
WARNING_DISABLE("-Wsome-warning")
...
}
Is there any C/C++ syntax that will force a semi-colon in the parser at any point in a file that doesn't have side effects?
Edit: A possible use case:
#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__ {}
#define DETAIL(warning) _Pragma(#warning) struct X ## __LINE__ {}
You don't need LINE trick - it is enough to forward-declare some structure, which is allowed multiple times and there is no need for actual definition. Also collision with actual struct should not be a problem.
#define DETAIL(warning) _Pragma(#warning) struct dummy
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With