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

How to move a unique_ptr from one set to another? (C++)

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

谁能解释一下这是为什么?我原以为一切都会好起来的,因为我在 foo.add.

的调用中使用了 std::move
#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++ 17 extract() function.

example

#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());
}