相关疑难解决方法(0)

从std :: set中提取move only类型

我有一个std::set<std::unique_ptr<T>>,我想把它移到一个std::vector<std::unique_ptr<T>>

#include <set>
#include <vector>
#include <memory>

class C {};

int main()
{
  std::set<std::unique_ptr<const C>> s;
  std::vector<std::unique_ptr<const C>> v;
  std::move(s.begin(), s.end(), std::back_inserter(v));
}
Run Code Online (Sandbox Code Playgroud)

这会在VS2017上出现以下错误:

错误C2280:'std :: unique_ptr> :: unique_ptr(const std :: unique_ptr <_Ty,std :: default_delete <_Ty >>&)':尝试引用已删除的函数

我们不能将迭代器移动到非const变量std::set吗?什么可以解决这个问题?

c++ stl move set c++11

3
推荐指数
1
解决办法
1250
查看次数

标签 统计

c++ ×1

c++11 ×1

move ×1

set ×1

stl ×1