我正在尝试在我的项目中运行 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/