以下代码将无法在大多数编译器上编译:
#include <type_traits>
class Foo
{
public:
Foo() noexcept {}
~Foo() noexcept(false) {}
};
static_assert(std::is_nothrow_default_constructible_v<Foo>);
CppReference还指出,这在编译器实现中很常见,但没有其他选择。如何在不影响析构函数结果的情况下测试构造函数是否为noexcept?
如LWG第2116期中所述,从您链接的cppreference页面进行链接,这不是bug,而是预期的行为。
就像在该问题中提到的那样,可以使用仅构造但不破坏对象的非抛出放置新函数来测试仅构造函数的异常规范:
static_assert(noexcept(::new(std::nothrow) Foo()));
这需要包括<new>
for 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> { };
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句