c++ - Visual Studio for C++ __cplusplus 支持的语言显示为 C++98

标签 c++ visual-studio c++11 visual-c++

我正在尝试在我的项目中运行 C++11。我将编译器标志 /std:c++11 添加到编译器,但是当我检查版本并打印出来时,它显示为 C++98。这是在 Visual Studio 2019 中。

这是我用来打印语言的,它显示为 C++98:

if (__cplusplus == 201703L) std::cout << "C++17\n";
else if (__cplusplus == 201402L) std::cout << "C++14\n";
else if (__cplusplus == 201103L) std::cout << "C++11\n";
else if (__cplusplus == 199711L) std::cout << "C++98\n";
else std::cout << "pre-standard C++\n";

std::cout << "C++ langauge supported = " << __cplusplus << "\n";

最佳答案

/Zc:__cplusplus需要为 __cplusplus 宏打开正确的版本控制。

但是请注意,编译器不支持 C++98、C++03 或 C++11 的标准开关。因此它只能与 /std 一起使用:c++14 及更高版本。

正如 Ted Lyngmo 所提到的,还有 _MSVC_LANG 宏(这不需要上面的编译器标志):

_MSVC_LANG Defined as an integer literal that specifies the C++ language standard targeted by the compiler. It's set only in code compiled as C++. The macro is the integer literal value 201402L by default, or when the /std:c++14 compiler option is specified. The macro is set to 201703L if the /std:c++17 compiler option is specified. It's set to a higher, unspecified value when the /std:c++latest option is specified. Otherwise, the macro is undefined.

请参阅此 Visual C++ blog post有关此行为和新开关的更多背景信息。

关于c++ - Visual Studio for C++ __cplusplus 支持的语言显示为 C++98,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63057156/

相关文章:

c++ - C/C++ : with int i[3], 为什么 i[2] 与 2[i] 相同?

即使有种子,C++ <random> 分布也不是随机的

c - C Windows 中的简单链表

c++11 - c++ 中的 vigenere 密码

c++ - Sublime 上的 EasyClangComplete 有什么用?

c++ - Visual Studio _MSC_VER 与平台工具集

.net - 在 Visual Studio 中折叠所有文档标题/注释的快捷方式

c++ - 如何使用GYP?

c++ - 在 C++ 中使用 erase-remove 习语时,我应该通过引用传递吗?

c++ - 特殊成员函数是 noexcept 还是 throw()?