如何将unique_ptr从一组移动到另一组?(C++)

下面的代码给出了错误:Call to deleted constructor of 'std::unique_ptr<int>' 'unique_ptr' has been explicitly marked deleted here passing argument to parameter 'item' here

有人可以解释一下这是为什么吗?我本以为一切都会好起来的,因为我std::move在调用foo.add.

#include <iostream>
#include <memory>
#include <set>

class Foo {
 public:
  void add(std::unique_ptr<int> item) {
    set.emplace(std::move(item));
  }
 private:
  std::set<std::unique_ptr<int>> set;
};

int main() {
  Foo foo;

  std::set<std::unique_ptr<int>> set;
  set.emplace(std::make_unique<int>(1));
  set.emplace(std::make_unique<int>(2));
  set.emplace(std::make_unique<int>(3));

  for (auto &item : set) {
    foo.add(std::move(item)); // error on this line
  }

  return 0;
}

回答

使用C++ 17extract()函数。

例子

#include <set>
#include <memory>
#include <iostream>

int main() {
    auto s = std::set<std::unique_ptr<int>>{};

    s.insert(std::make_unique<int>(10));

    std::cout << s.size() << "n";

    auto it = s.extract(s.begin());

    // Pointer type here just for clarification
    std::unique_ptr<int> new_ptr = std::move(it.value());
    
    std::cout << s.size() << "n";
    
    std::cout << *new_ptr << "n";
}

然后,您可以使用 while 循环代替 for each 循环:

while (!set.empty()) {
   auto it = set.extract(set.begin());
   foo.add(std::move(it.value());
}


以上是如何将unique_ptr从一组移动到另一组?(C++)的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>