【发布时间】:2018-06-19 13:02:24
【问题描述】:
以下代码无法使用 Visual Studio 2017 (15.5)、gcc 6.4.0 和 clang 4.0.1 编译,即静态断言失败:
struct Type
{
Type(Type&&) noexcept {}
~Type() noexcept(false) {}
};
static_assert(std::is_nothrow_move_constructible<Type>::value, "Type should be nothrow-move-constructible");
static_assert(std::is_nothrow_constructible<Type, Type&&>::value, "Type should be nothrow-constructible from Type&&");
C++ 标准是否正确? std::is_nothrow_move_constructible 是否需要 noexcept 析构函数?为什么?
如果我这样使用:
Type a;
Type b(std::move(a));
a 的析构函数在第二条语句中没有被调用。
【问题讨论】:
-
仅供参考:跟随链(以 en.cppreference.com/w/cpp/types/is_move_constructible 开头)
std::is_nothrow_move_constructible->std::is_nothrow_constructible您可以在此处看到此注释:“在许多实现中,is_nothrow_constructible 还检查析构函数是否抛出...” en.cppreference.com/w/cpp/types/is_constructible -
@RichardCritten 感谢您的提示!由于此显式引用“许多实现”,因此听起来此行为不符合标准。如果标准将此作为实现的选择,我会感到惊讶。
标签: c++ c++11 language-lawyer typetraits