3

の 2 つのベクトルをマージしようとしていますunique_ptr(つまり、1 つのベクトルstd::moveから別のベクトルへ)。「削除された関数の使用...」というエラー テキストの壁に遭遇し続けます。エラーによると、unique_ptr削除された のコピー コンストラクターを使用しようとしているようですが、その理由はわかりません。以下はコードです:

#include <vector>
#include <memory>
#include <algorithm>
#include <iterator>

struct Foo {
    int f;

    Foo(int f) : f(f) {}
};

struct Wrapper {
    std::vector<std::unique_ptr<Foo>> foos;

    void add(std::unique_ptr<Foo> foo) {
        foos.push_back(std::move(foo));
    }

    void add_all(const Wrapper& other) {
        foos.reserve(foos.size() + other.foos.size());

        // This is the offending line
        std::move(other.foos.begin(), 
                  other.foos.end(), 
                  std::back_inserter(foos));
    }
};

int main() {
    Wrapper w1;
    Wrapper w2;

    std::unique_ptr<Foo> foo1(new Foo(1));
    std::unique_ptr<Foo> foo2(new Foo(2));

    w1.add(std::move(foo1));
    w2.add(std::move(foo2));

    return 0;
}
4

1 に答える 1