2010-04-21 17 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; 
} 

derleme değil ...Derleme için unique_ptr ile ilgili bu kodu nasıl alabilirim?

ben ileri ile hamle yerine çalıştı (unique_ptr en yapıcı silinir kopya olduğunu söylüyor). Doğru yaptığımı bilmiyordum, ama benim için çalışmadı.


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

hatayı belirtin. –

+0

tamamlandı. (gcc 4.4.0) –

cevap

3

Muhtemelen standart kitaplığı (henüz) unique_ptr<T>::unique_ptr(unique_ptr &&) tanımlamıyor. Başlıklarımı 4.5'te kontrol ettim ve var, belki yükseltmeyi dene.

Taşıyıcıyı bulmak başarısız olduğunda, kopya kurucuyu arar ve silinmiş olduğunu bulur.

Bunu derlerken başka hatalar alıyorum.

DÜZENLEME: Çalıştırdım. Neden zaten bir rvalue referansı olan bir nesneyi move'a sahip olduğunuzu anlamıyorum, ancak siz yapıyorsunuz. Tek sorun eksik bir atama operatörü oldu.

#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

Vay canına! Çözümünüz için çok minnettarım. Bu beni hiç bitmeyecek kadar sinirlendiriyordu. –

+0

Çözümünüzü test ettik ve çalışıyor. Gerçekten de, operatörün eşleşen kopya kurucuyu kullanarak varsayılan olarak ayarlanmasını isterim. Belki "oto kavramları" ile, onlar olacak? –

+1

Muhtemelen bundan benden daha çok şey biliyorsunuz; Bunu bir hata mesajı jokey olarak çözdüm. – Potatoswatter