微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 在功能之外吞下分号的宏

有没有一个成语在功能之外的cpp宏之后强制分号?

函数内部使用的宏的已知解决方案是:

#define MACRO(x) \
  do {
    x * 2;
  } while(0)

不过说我有一个如下所示的宏:

#define DETAIL(warning) _Pragma(#warning)
#define WARNING_disABLE(warning) DETAIL(GCC diagnostic ignore warning)

我可以把那个强制在分析结束之后的分号放在宏中.该声明可以在函数中或外部使用:

WARNING_disABLE("-Wunused-local-typedefs")
#include "boost/filesystem.hpp"
void foo(const int x) {
    WARNING_disABLE("-Wsome-warning")
    ...
}

是否有任何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__ {}

解决方法

#define DETAIL(warning) _Pragma(#warning) struct X ## __LINE__ {}

原文地址:https://www.jb51.cc/c/113334.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐