此代码为 Visual Studio 2012 和 2008 输出 T2、T4,为 gcc 输出 T1、T2、T3、T4。 这是什么原因?
#include <iostream>
#define ABC
#define T1 defined(ABC)
#define T2 defined( ABC )
#define T3 defined(ABC )
#define T4 defined( ABC)
int main(int argc, char* argv[])
{
#if T1
std::cout<<"T1"<<std::endl;
#endif
#if T2
std::cout<<"T2"<<std::endl;
#endif
#if T3
std::cout<<"T3"<<std::endl;
#endif
#if T4
std::cout<<"T4"<<std::endl;
#endif
return 0;
}
最佳答案
查看conditional directives页。我发现:
The defined directive can be used in an #if and an #elif directive, but nowhere else.
将您的代码更改为:
#include <iostream>
#define ABC
int main(int argc, char* argv[])
{
#if defined(ABC)
std::cout << "T1" << std::endl;
#endif
#if defined( ABC )
std::cout << "T2" << std::endl;
#endif
#if defined(ABC )
std::cout << "T3" << std::endl;
#endif
#if defined( ABC)
std::cout << "T4" << std::endl;
#endif
return 0;
}
将在 VS 2013 中生成 T1,T2,T3,T4
输出
关于C++ 奇怪的行为 Visual Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29009311/