C ++奇怪行为Visual Studio

算术

对于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;
}
gdbcore

查看条件指令页面。我发现:

可以在#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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章