【发布时间】:2018-12-10 14:15:15
【问题描述】:
我想取消定义并重新定义 __cplusplus 宏,但是编译错误:__cplusplus was not declared in this scope
#define TEMP_VERSION __cplusplus // temporary macro to hold the __cplusplus containt
#undef __cplusplus
#define __cplusplus TEMP_VERSION // redefine __cplusplus
#define AA 111
#undef AA
#define AA 111
int main()
{
cout << "__cplusplus = "<<__cplusplus<<endl; // It doesn't work
cout << "AA = "<<AA<<endl; // It works
return 0;
}
- 问题 1:为什么它不能与
__cplusplus一起使用,但与AA一起使用
我知道这真的很难看,但我未定义宏的原因是我正在使用第三方软件,他们误用了#ifdef __cplusplus,我的意思是#ifdef __cplusplus ... #endif 的内容是错误的。因此,我没有更改他们的软件,而是选择执行以下操作
#define TEMP_VERSION __cplusplus
#undef __cplusplus
#include <third_party_sw.h>
#define __cplusplus TEMP_VERSION
- 问题 2:您认为这是一种好的方法吗?
【问题讨论】:
-
以双下划线开头的名称保留用于实现。从技术上讲,这是 UB。
-
因为 'AA' 被定义为在 '
-
“#ifdef __cplusplus ... #endif 的内容是错误的” 你可能应该询问一个适当的解决方法(给出一些关于什么是错误的细节) , 现在这是一个典型的 XY 问题
-
“它适用于
AA” 你使用AA的方式不同。考虑:coliru.stacked-crooked.com/a/06065c268a7c2bff