Как я могу получить этот код с использованием unique_ptr для компиляции? - PullRequest
5 голосов
/ 22 апреля 2010
#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;
}

не компилируется ... (говорит, что конструктор копирования unique_ptr удален)

Я попытался заменить move на forward.Не уверен, правильно ли я это сделал, но у меня это не сработало.


[~/nn/src] g++ a.cc -o a -std=c++0x
/opt/local/include/gcc44/c++/bits/unique_ptr.h: In member function 'A& A::operator=(const A&)':
a.cc:6:   instantiated from 'void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, _Args&& ...) [with _Args = A, _Tp = A, _Alloc = std::allocator<A>]'
/opt/local/include/gcc44/c++/bits/vector.tcc:100:   instantiated from 'void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = A, _Tp = A, _Alloc = std::allocator<A>]'
a.cc:17:   instantiated from here
/opt/local/include/gcc44/c++/bits/unique_ptr.h:219: error: deleted function 'std::unique_ptr<_Tp, _Tp_Deleter>& std::unique_ptr<_Tp, _Tp_Deleter>::operator=(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = int, _Tp_Deleter = std::default_delete<int>]'
a.cc:6: error: used here
In file included from /opt/local/include/gcc44/c++/vector:69,
                 from a.cc:1:
/opt/local/include/gcc44/c++/bits/vector.tcc: In member function 'void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, _Args&& ...) [with _Args = A, _Tp = A, _Alloc = std::allocator<A>]':
/opt/local/include/gcc44/c++/bits/vector.tcc:100:   instantiated from 'void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = A, _Tp = A, _Alloc = std::allocator<A>]'
a.cc:17:   instantiated from here
/opt/local/include/gcc44/c++/bits/vector.tcc:314: note: synthesized method 'A& A::operator=(const A&)' first required here

1 Ответ

3 голосов
/ 22 апреля 2010

Возможно, ваша стандартная библиотека (пока) не определяет unique_ptr<T>::unique_ptr(unique_ptr &&). Я проверил свои заголовки в 4.5, и это там, так что, возможно, попробуйте обновить.

Когда не удается найти конструктор перемещения, он ищет конструктор копирования и находит его удаленным.

Однако при компиляции я получаю другие ошибки.

РЕДАКТИРОВАТЬ: Получил его на работу. Я не понимаю, почему вы должны move объект, который уже является ссылкой на значение, но вы делаете. Единственной проблемой был отсутствующий оператор присвоения.

#include <vector>
#include <memory>

using namespace std;

class A {
public:
    A(): i(new int) {}
    A(A const& a) = delete;
    A &operator=(A const &) = delete;
    A(A &&a): i(move(a.i)) {}
    A &operator=(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;
}
...