以下代码将无法在大多数编译器上编译:
#include <type_traits>
class Foo
{
public:
Foo() noexcept {}
~Foo() noexcept(false) {}
};
static_assert(std::is_nothrow_default_constructible_v<Foo>);
CppReference也states,这在编译器实现中很常见,但没有其他选择。如何在没有析构函数影响结果的情况下测试构造函数是否为noexcept?
如LWG issue 2116中所述,从您链接的cppreference页面链接,这不是错误,而是预期的行为。
也正如在该问题中提到的那样,可以使用仅构造但不破坏对象的非抛出放置新函数来仅测试构造函数的异常规范:
static_assert(noexcept(::new(std::nothrow) Foo()));
这需要在<new>
中包含std::nothrow
。
这里是特征的粗略实现:
template<class T, class = void>
struct is_nothrow_default_constructible
: std::bool_constant<false> { };
template<class T>
struct is_nothrow_default_constructible<T,
std::enable_if_t<noexcept(::new(std::nothrow) T())>>
: std::bool_constant<true> { };