首页 > 解决方案 > 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 中是完整的,所有定义都放置在其中。

标签: c++boostcompiler-errorsmovecopy-constructor

解决方案


推荐阅读