How can I get this code involving unique_ptr and emplace_back to compile?

Posted by Neil G on Stack Overflow See other posts from Stack Overflow or by Neil G
Published on 2010-04-21T23:55:10Z Indexed on 2010/04/22 0:33 UTC
Read the original article Hit count: 427

Filed under:
#include <vector>
#include <memory>

using namespace std;

class A {
public:
    A(): i(new int) {}
    A(A const& a) = delete;
    A(A &&a): i(move(a.i)) {}

    unique_ptr<int> i;
};

class AGroup {
public:
    void                    AddA(A &&a) { a_.emplace_back(move(a)); }

    vector<A> a_;
};

int main() {
    AGroup ag;
    ag.AddA(A());
    return 0;
}

does not compile... (says that unique_ptr's copy constructor is deleted)

I tried replacing move with forward. Not sure if I did it right, but it didn't work for me.

© Stack Overflow or respective owner

Related posts about c++0x