如何在 MSVC 下检测 C++11 的 noexcept 特性?

How to detect C++11 under MSVC for noexcept feature?

我正在使用 C++ 库。该库的最低要求是 C++03。我在 Visual Studio 2015 下收到了一些关于抛出析构函数的警告:

... algparam.h(271): warning C4297: 'AlgorithmParametersBase::~AlgorithmParametersBase':
    function assumed not to throw an exception but does
... algparam.h(271): note: destructor or deallocator has
    a (possibly implicit) non-throwing exception specification

抛出是在 1990 年代编写代码时设计的,但它现在显示出它的年代。我们目前有点陷入困境,因为我们还没有准备好迎接主要版本的提升。 (而且它并不像听起来那么糟糕 prima fascia 因为它根据 uncaught_exception 来防守投掷)。

我想通过检测 C++ 11 来解决问题,然后添加 noexcept(false)。我打算将它隐藏在宏中,以便代码可以在 C++03 和 C++11 下编译。

以下 not 在 Visual Studio 2015 下检测 C++11。它不会产生预期的结果编译器错误:

#if (__cpluplus >= 201103L)
# error Not C++11
#endif

此外,Microsoft not 似乎提供 Predefined Macros 来检测语言功能。

如何在 Visual Studio 2015 下检测 C++11?


相关,我可以通过_MSC_VER检测MSVC编译器的版本。我 认为 VS2015 将是 _MSC_VER=1900。但是它没有告诉我任何关于语言特性的信息。

如果用户使用 VS2015,但使用下层 C++ 标准,则语言功能很重要。另外,我没有要测试的 VS2013,所以我不确定像 _MSC_VER=1700 这样的 1-off 字符串是个好主意。我更愿意了解它是否是 C++11。


我 运行 在 OS X 和 Linux 上使用 Clang 和 GCC 编译器遇到了类似的问题。例如,参见 How to guard move constructors for C++03 and C++11? and What differences, if any, between C++03 and C++11 can be detected at run-time? 但在这种情况下,我想通过预处理器检测它。


最后,这个库没有使用 Autotools、Cmake 或 Boost。它没有外部依赖性。

您无法独立于 VS 版本检测语言版本,因为 VS 不提供语言版本控制开关 - 没有 VS2015 C++03 模式这样的东西。您尝试检测的条件(VS2015 的下层标准)不存在,这很容易解释为什么没有功能宏来检测它。

只需检测 VS 版本并关闭它。