使用_MSVC_LANG而不是__cplusplus是否安全? [英] Is it safe to use _MSVC_LANG instead of __cplusplus?
问题描述
我正在尝试使用Microsoft Visual C ++编译器(以前使用GCC和其他编译器)来编译多平台C ++项目。
I am trying to compile a multiplatform C++ project with the Microsoft Visual C++ compiler (formerly the GCC was used, among other compilers).
现在我遇到了一些这样的预处理器指令:
Now I come across some preprocessor directives like this one:
#if __cplusplus < 201103L
// Define some macros of C++11 the code really relies on.
// [...]
#endif
尽管我使用Visual Studio 2015 __ cplusplus
仍被定义为 199711L
。 来自微软的帖子博客建议也检查 _MSVC_LANG
。
Although I use Visual Studio 2015, __cplusplus
is still defined as 199711L
. This post from the Microsoft blog advises to check also for _MSVC_LANG
.
在什么程度上_MSVC_LANG> = 201402L
不符合C ++ 11吗?
To what extent does _MSVC_LANG >= 201402L
not comply with C++11 ?
推荐答案
首先,如果需要便携式解决方案,您可以执行以下操作:
First, if you want a portable workaround, you can do:
#if __cplusplus < 201103L && _MSVC_LANG < 201103L
/* ... */
#elif __cplusplus >= 201402L || _MSVC_LANG >= 201402L
您链接的注释指出,这是 __ cplusplus $的错误c $ c>设置不正确,测试
_MSVC_LANG
是权宜之计。但是,具有 / std:c ++ 14
的VC 2017(19.10.25017)仍将 __ cplusplus
设置为 199711
。我不确定这是否意味着对C ++ 14的支持还没有完全完成,或者他们只是从没接受过。
The comment you link states that it’s a bug that __cplusplus
is not set correctly and that testing _MSVC_LANG
is a stopgap. However, VC 2017 (19.10.25017) with /std:c++14
still sets __cplusplus
to 199711
. I’m not sure whether that means C++14 support is still not entirely complete, or if they just never got around to it.
_MSVC_LANG
宏是Microsoft的扩展程序。大多数其他编译器均未对其进行设置,以使其更容易测试该编译器是否为Microsoft。 (一个例外: clang ++ --std:c ++ 14 -fms-compatibility-version = 19.10
将同时设置 __ cplusplus
和 _MSVC_LANG
到 201402L
,因为这是其MSVC兼容模式。)
The _MSVC_LANG
macro is a Microsoft extension. Most other compilers do not set it, to make it easier to test that the compiler is or is not Microsoft. (One exception: clang++ --std:c++14 -fms-compatibility-version=19.10
will set both __cplusplus
and _MSVC_LANG
to 201402L
, as that is its MSVC compatibility mode.)
ETA::感谢一位匿名评论员指出,存在一个 / Zc:__ cplusplus
编译器开关,该开关设置了 __ cplusplus
来匹配您选择的变体。
ETA: Thanks to an anonymous commenter for pointing out that there is a /Zc:__cplusplus
compiler switch, which sets the value of __cplusplus
to match the variant you select.
这篇关于使用_MSVC_LANG而不是__cplusplus是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!