C++ noexcept(false)析构函数重写所有特殊成员函数';异常规范?
考虑这个类C++ noexcept(false)析构函数重写所有特殊成员函数';异常规范?,c++,gcc,visual-c++,clang,language-lawyer,C++,Gcc,Visual C++,Clang,Language Lawyer,考虑这个类T struct T{ T() noexcept (true) {} T(T&& ) noexcept (true) {} T(const T& ) noexcept (true) {} T& operator = (T&&) noexcept(true) { return *this; } T& operator = (const T&) noexcept(
T
struct T{
T() noexcept (true) {}
T(T&& ) noexcept (true) {}
T(const T& ) noexcept (true) {}
T& operator = (T&&) noexcept(true) { return *this; }
T& operator = (const T&) noexcept(true) { return *this; }
~T() noexcept(false) {}
};
考虑这个简单的测试程序:
int main(){
constexpr bool default_ctor = noexcept(T());
static_assert(default_ctor == true, "Default Constructor can throw exceptions");
constexpr bool move_ctor = noexcept(T(std::declval<T>())); //Move ctor
static_assert(move_ctor == true, "Move Constructor can throw exceptions");
constexpr bool copy_ctor = noexcept(T(std::declval<T&>())); //Copy ctor
static_assert(copy_ctor == true, "Copy Constructor can throw exceptions");
constexpr bool move_assign = noexcept(std::declval<T>() = std::declval<T>());
static_assert(move_ctor == true, "Move Assignment can throw exceptions");
constexpr bool copy_assign = noexcept(std::declval<T&>() = std::declval<const T&>());
static_assert(copy_ctor == true, "Copy Assignment can throw exceptions");
//It doesn't matter when using *`type_traits`* either. Same behavior:
constexpr bool no_throw_cons = std::is_nothrow_constructible<T>::value;
static_assert(no_throw_cons == true, "Default Constructor isn't nothrow");
//...others skipped for brevity
}
std::terminate
按预期调用
< > C++的标准有没有定义或暗示这种行为的任何部分?析构函数上的
noexcept(false)
说明符仅在编译时重写每个特殊成员函数的异常规范
或者这是每个主要编译器中的前端错误。在第一次测试中,您会询问完整表达式
T()
是否会引发异常。该表达式构造一个T
,然后再次销毁它。因此,如果析构函数可以抛出,那么表达式也可以抛出。Ohh。。。哎呀!我完全忘记了充分表达。谢谢我也看到了。但是,注释暗示,如果T
的析构函数是noexcept(true)
,那么类型traitstd::no_throw\u constructible::value
和it friends是否为true
;对于编译器供应商来说,这是一个无限制的决定,对吗?这意味着我们不能完全依赖这种行为来处理这种情况。谢谢(很抱歉把评论弄错了)
struct T{
T() noexcept (true) { throw int(8); }
//.... there rest omitted for brevity
~T() noexcept(false) {}
};
int main(){
T a;
(void)a;
};