C++ 奇怪的行为 Visual Studio

标签 c++ visual-studio visual-studio-2012 defined

此代码为 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/

相关文章:

c++ - 如何避免out参数?

c++ - 'Excel.exe' 的调试信息找不到或不匹配

c++ - 是否可以为我的 cocos2d-x 游戏构建一个 .exe 文件?

visual-studio - Xamarin Android 调试 : No compatible code running

无法在 C 中使用 * void 指针初始化结构

c++ - 新运算符(以及 malloc)无法分配 ~ 450 MB 内存

c++ - 模型转换

c++ - 使用 vector 时出现奇怪的运行时错误

visual-studio-2012 - 未找到错误 MSB4019 : Microsoft. WindowsPhone.v4.5.Overrides.targets”

debugging - 如何在远程调试时查看本地变量和自动变量值