c++ - 为什么我的默认移动构造函数不是 noexcept?
问题描述
根据this questionnoexcept
的答案,可以在某些条件下定义默认移动构造函数。例如,下面的类生成一个noexcept
移动构造函数:
class C {};
根据这个问题的答案,使用说明= default
符定义的移动构造函数将生成与隐式定义的移动构造函数相同的构造函数。所以,如果我理解正确的话,下面的类应该生成一个noexcept
移动构造函数:
class D {
D(D&&) = default;
};
为了检查这一点,我使用该std::is_nothrow_move_constructible
函数来查看是否C
有D
一个noexcept
移动构造函数:
#include <type_traits>
int main() {
static_assert(std::is_nothrow_move_constructible<C>::value, "C should be noexcept MoveConstructible");
static_assert(std::is_nothrow_move_constructible<D>::value, "D should be noexcept MoveConstructible");
return 0;
}
当我编译时,我得到这个错误:
$ g++ toy.cpp -o toy
toy.cpp: In function ‘int main()’:
toy.cpp:16:5: error: static assertion failed: D should be noexcept MoveConstructible
static_assert(std::is_nothrow_move_constructible<D>::value, "D should be noexcept MoveConstructible");
^~~~~~~~~~~~~
为什么我的D
移动构造函数不是noexcept
?
我也尝试了 Clang,我得到了同样的错误。以下是有关我的编译器的信息:
$ g++ --version
g++ (Debian 6.3.0-18+deb9u1) 6.3.0 20170516
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ clang++8 --version
clang version 8.0.0
Target: x86_64-unknown-linux-gnu
Thread model: posix
解决方案
noexcept
事实上,它与;无关。static_assert
也会失败,std::is_move_constructible
因为移动构造函数是private
. 因此,只需将其声明为public
.
class D {
public:
D(D&&) = default;
};
推荐阅读
- javascript - Pug - 如何将带有类的 span 标签添加到 mixins 参数中
- jenkins - 来自不同 Jenkins 工作的 github pull request 的摘要评论
- python - 更新 QDockWidget 的子小部件,这些小部件在鼠标悬停时自然会发生
- ios - 界面生成器中的错误?安全区域超出窗口范围
- anylogic - Anylogic:如何单独寻址资源池中的资源
- android - 如何在 Android Oreo 的特定时间在 Android 上发出通知?
- r - 闪亮的动态selectizeInput
- regex - 语义 UI:使用正则表达式进行密码验证
- matlab - 梯度下降总是趋于无穷
- dart - Flutter:在 Dart 中获取多个 TextFormField 的值