c++ - 未生成隐式移动函数

标签 c++ llvm c++11

我有以下类(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/

相关文章:

C++ 编译错误

compiler-construction - 编译器输出语言 - LLVM IR vs C

c++ - 在 LLVM 3.9.1 中,我应该用什么替换 getGlobalContext()?

c++ - 将 google protobuf lib 静态链接到 dll 库中

c++ - C/C++ argv 内存管理

apache - 使用 LLVM Plus 自定义 channel 和自定义库编译 Apache Server

c++ - 是否可以强制 STL 集重新评估谓词?

c++ - 从原始指针创建 shared_ptr 的链表

c++ - 为什么这个程序在 C++14 中编译得很好,但在 C++11 编译器中却不行?

c++ - 奇怪的编译问题 - 无法识别的类没有编译器错误