Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macro that swallows semicolon outside of function

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__ {}
like image 624
Matt Clarkson Avatar asked Sep 13 '13 12:09

Matt Clarkson


2 Answers

#define DETAIL(warning) _Pragma(#warning) struct X ## __LINE__ {}
like image 142
Angew is no longer proud of SO Avatar answered Nov 09 '22 15:11

Angew is no longer proud of SO


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
like image 24
user2224044 Avatar answered Nov 09 '22 15:11

user2224044