对于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;
}
查看条件指令页面。我发现:
可以在#if和#elif指令中使用已定义的指令,但无其他地方。
将您的代码更改为:
#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;
}
将T1,T2,T3,T4
在VS 2013中产生输出
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句