C2694 在析构函数上,当基类成员的析构函数具有非空 noexcept 说明符和主体时

C2694 on destructor when base class' member's destructor has non-empty noexcept specifier and a body

本文关键字:析构函数 noexcept 说明符 主体 基类 C2694 成员      更新时间:2023-10-16

我遇到了一个编译器错误,我无法解释,也无法在线找到有关它的信息。我最近在包装类的析构函数中添加了一个noexcept说明符,现在大量从使用此包装器的类继承的类都无法编译。我已经用GCC 4.9尝试过,没有编译器错误。

我正在使用 Visual Studio Professional 2015 版本 14.0.25431.01 Update 3

考虑以下重现问题的最小代码:

#include <type_traits>
template<class T>
struct member
{
~member() noexcept(std::is_nothrow_destructible<T>::value) {};
};
struct parent
{ 
virtual ~parent() noexcept = default;
};

struct derived : public parent
{
member<int> x;
};

该代码段生成以下错误消息:

1>c:usersfandrieuxworkspacetmpdtor_noexceptmain.cpp(19): error C2694: 'derived::~derived(void) noexcept(<expr>)': overriding virtual function has less restrictive exception specification than base class virtual member function 'parent::~parent(void) noexcept'
1>  c:usersfandrieuxworkspacetmpdtor_noexceptmain.cpp(19): note: compiler has generated 'derived::~derived' here
1>  c:usersfandrieuxworkspacetmpdtor_noexceptmain.cpp(12): note: see declaration of 'parent::~parent'

我发现有趣的是,如果您通过= default替换成员的析构函数主体或使用noexceptnoexcept(true),编译器错误就会消失:

// None of these produce compiler errors
virtual ~member() noexcept(std::is_nothrow_destructible<T>::value) = default;
virtual ~member() noexcept(true) {}
virtual ~member() noexcept {}

我知道它是析构函数不会抛出。偏执狂和怀疑论者(像我一样)可以添加以下静态断言并自行检查:

static_assert(std::is_nothrow_destructible<T>::value, "Might throw!");

根据 MSDN,它表示动态异常说明符不足。这在这里如何应用?noexcept([boolean expression])不等同于noexcept(true)noexcept(false)吗?为什么这会根据函数体的存在而变化?将显式 noexcept 析构函数添加到派生中会导致编译器错误,但这感觉像是一种不必要的解决方法。实际上,当您考虑到每个派生类都必须更新时,这也是一个相当大的负担。

这看起来像一个编译器错误。如果我们添加以下类:

struct dtor_throws
{
~dtor_throws() noexcept(false) {}
};

并因此更改derived的定义:

struct derived : public parent
{
member<dtor_throws> x;
};

然后GCC和Clang都抱怨~derived的异常规范比~parent宽松。

在原始示例中,MSVC似乎没有将noexcept表达式的值嵌入到~parent的类型中,而只是将类模板的用户定义析构函数的所有复合noexcept规范视为比noexcept(true)更宽松。

MSVC 2017 RC也受到影响。