2010-04-21 18 views
5
#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; 
} 

no se compila ... (dice que unique_ptr de copia se elimina constructor)¿Cómo puedo obtener este código que implica unique_ptr para compilar?

que intentaron reemplazar movimiento con adelante. No estoy seguro si lo hice bien, pero no funcionó para mí.


[~/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 
+0

Por favor, incluya el error. –

+0

hecho. (gcc 4.4.0) –

Respuesta

3

Probablemente su biblioteca estándar no (todavía) definen unique_ptr<T>::unique_ptr(unique_ptr &&). Revisé mis encabezados en 4.5 y está allí, así que tal vez intente actualizar.

Cuando no encuentra el constructor de movimientos, busca el constructor de copias y lo encuentra eliminado.

Obtengo otros errores cuando compilo eso, sin embargo.

EDITAR: Lo tengo que trabajar. No entiendo por qué tiene que move un objeto que ya es una referencia rvalue, pero lo hace. El único problema era un operador de asignación faltante.

#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; 
} 
+0

¡Guau! Estoy muy agradecido por tu solución. Esto me estaba frustrando sin fin. –

+0

Probó su solución y funciona. Realmente me gustaría que el operador = de manera predeterminada utilizara el constructor de copia correspondiente. Tal vez con 'auto conceptos ', ¿lo harán? –

+1

Probablemente sepa más sobre eso que yo; Lo resolví siendo un jinete de mensajes de error. – Potatoswatter

Cuestiones relacionadas