小编Aak*_*pta的帖子

C++ 中 #ifdef Defined() 的奇怪行为

我正在 Visual Studio Community 中使用 C++20 编写跨平台代码,但我陷入了以下代码的输出:


#define WINDOWS (defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__))
#define UNIX (defined(__unix__) || defined(__unix))

constexpr bool is_unix1()
{
#ifdef UNIX
    return true;
#else
    return false;
#endif
}

constexpr bool is_unix2()
{
#ifdef defined(UNIX)
    return true;
#else
    return false;
#endif
}


int main()
{
    cout << std::boolalpha << is_unix1() << " " << is_unix2() << endl;
}
Run Code Online (Sandbox Code Playgroud)

当我在 Windows 中从 VS Community 内部运行此代码时,我得到以下输出:

true false
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么is_unix1()评估为falsewhileis_unix2()评估为吗true? …

c++ macros conditional-compilation defined

0
推荐指数
1
解决办法
125
查看次数

标签 统计

c++ ×1

conditional-compilation ×1

defined ×1

macros ×1