未生成隐式移动函数
我有以下课程:
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&&)' 不能默认
遗憾的是没有原因。谁能解释一下为什么失败
解决方案: 实际上我使用dragonegg插件来生成llvm代码。禁用dragonegg并使用普通的g++默认工作正常。 查看 g++ 的源代码,不能默认(使用移动分配)消息是 4.5 (module.c) 中的一个错误,但在 4.6 中得到修复。?由于 Dragonegg 确实依赖于 g++ 4.5,我怀疑修复尚未完成。真糟糕。
I have the following 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;
};
For some strange reason I had to force the move constructor.
Now trying to force the move assignment operator G++ (4.6.1) chokes with: error: 'Blub& Blub::operator=(Blub&&)' cannot be defaulted
Sadly there is no WHY. Could anyone shed some light why this is failing
The Solution:
Actually I use the dragonegg plugin to generate llvm code. Disabling dragonegg and using normal g++ the defaulting works fine.
Looking at the source of g++ the cannot be defaulted (with move assignment) message was a bug in 4.5 (module.c) but got fixed in 4.6.?. Since dragonegg does depend upon g++ 4.5 I suspect that the fix is not in yet. Bummer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每当您显式声明复制构造函数或复制赋值运算符时,都必须强制使用移动构造函数,即使您
=默认
它,C++11也不会定义移动构造函数或赋值运算符。标准第 12.8 节(第 9 和 20 段)解释了未声明的规则。至于为什么默认的移动构造函数不起作用,我猜测是因为
Other
不支持移动操作。You have to force a move constructor anytime you explicitly declare a copy constructor or copy assignment operator, even if you
= default
it, C++11 will not define a move constructor or assignment operator for you. Section 12.8 of the standard (paragraphs 9 and 20) explain the rules for when these are not declared.As to why the default move constructor doesn't work, I'd guess that it's because
Other
doesn't support move operations.