我有以下类(class):
class Blub
{
public:
Blub(int value); // Not a copy constructor!
Blub(Blub&&) = default; // This line is necessary because move constructor is not added automatically
Blub& operator=(Blub&&) = default; // Does not work!?
// Disallow copy
Blub(Blub const &) = delete;
Blub& operator=(Blub const &) = delete;
};
出于某些奇怪的原因,我不得不强制移动构造函数。 现在尝试强制移动赋值运算符 G++ (4.6.1) 阻塞:error: 'Blub& Blub::operator=(Blub&&)' cannot be defaulted
遗憾的是没有为什么。谁能阐明为什么这会失败
解决方案: 实际上我使用 dragonegg 插件来生成 llvm 代码。禁用 dragonegg 并使用普通的 g++ 默认设置工作正常。 查看 g++ 的源代码,cannot be defaulted (with move assignment) 消息是 4.5 (module.c) 中的错误,但在 4.6.? 中得到修复。由于 dragonegg 确实依赖于 g++ 4.5,我怀疑修复尚未完成。真可惜。
最佳答案
无论何时显式声明复制构造函数或复制赋值运算符,您都必须强制移动构造函数,即使您= default
它,C++11 也不会定义一个移动构造函数或赋值运算符给你。标准的第 12.8 节(第 9 和 20 段)解释了不声明这些内容的规则。
至于为什么默认的移动构造函数不起作用,我猜是因为Other
不支持移动操作。
关于c++ - 未生成隐式移动函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7454209/