c++ - C2280:不可复制值类型的 boost::container::map 不能是 std::move()'d?
问题描述
我有一个不可复制的类Uncopyable
存储在boost::container::map<K, Uncopyable>
. 另一个类 ,Owner
有一个这样的映射对象。实施Owner::Owner(Owner&&)
需要搬家map
。但是,this->map = std::move(other.map)
在 Boost 中产生 C2280 pair.hpp
:“引用已删除的函数Uncopyable::Uncopyable(Uncopyable const&)
”。
这是最小的可重现示例。为简洁起见,省略了诸如五规则之类的一些代码方面。
// Owner.h:
#include <boost/container/map.hpp>
class Key;
class Uncopyable;
class Owner {
private:
boost::container::map<Key, Uncopyable> map = {};
public:
Owner(Owner&&);
Owner& operator=(Owner&&);
};
// Owner.cpp:
#include "Owner.h"
class Key {
};
class Uncopyable {
public:
Uncopyable(Uncopyable&&) {
}
Uncopyable& operator=(Uncopyable&&) {
return *this;
}
Uncopyable(Uncopyable const&) = delete;
Uncopyable& operator=(Uncopyable const&) = delete;
};
Owner::Owner(Owner&& other) {
*this = std::move(other);
}
Owner& Owner::operator=(Owner&& other) {
map = std::move(other.map);
return *this;
}
我忘了提到map
有一个Comparator
作为Key
第三个模板参数传递的。也许稍后会修复这个例子。
问题的原因是什么以及如何摆脱它?这是Boost中的错误吗?pair
的琐碎与此有某种联系吗?
如果它很重要,我正在使用 C++17 和(可能)最新的稳定版本的 Boost 和 MSVC。课堂Uncopyable
实际上是. std::variant<Copyable, Uncopyable>
在map
.h 中声明的类型不完整,在 .cpp 中是完整的,所有定义都放置在其中。
解决方案
推荐阅读
- postgresql - 如何通过postgreSQL中的bat文件禁用自动提交功能
- java - 有没有办法轻松调整 ANTLR4 的错误信息?
- java - 为什么 Robot.delay(int ms) 限制为 1 分钟?
- node.js - NodeJS Sharp 为 JPEG 图像提供了不正确的质量
- mongodb - Mongoose 保存修改后的用户数组
- java - 以编程方式忽略(省略)REST 服务的 JSON 响应中的特定字段,而不更改 DTO 对象类
- javascript - 在 Nodejs 中使用数据库引用作为全局变量或类似模块的单独服务?
- javascript - 如何根据所选选项更改表单操作?
- jquery - Dynamic change of fmt message key in JSP
- c# - 'Application_DocumentOpen' 没有重载匹配委托