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

컴파일되지 않습니다는 ...unique_ptr을 포함하는이 코드를 컴파일하려면 어떻게해야합니까?

내가 앞으로 함께 이동을 교체 시도 (unique_ptr의의 생성자가 삭제 복사 말한다). 내가 제대로했는지는 확실하지 않지만 그것은 나에게 도움이되지 못했다.


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

오류를 포함하십시오. –

+0

완료. (gcc 4.4.0) –

답변

3

아마 표준 라이브러리는 (아직) unique_ptr<T>::unique_ptr(unique_ptr &&)를 정의하지 않습니다. 4.5에서 헤더를 확인 했으므로 업그레이드가 필요합니다.

이동 생성자를 찾지 못하면 복사 생성자를 찾고 삭제 된 것을 찾습니다.

컴파일 할 때 다른 오류가 발생합니다.

편집 : 제대로 작동합니다. 나는 왜 당신이 move에 이미 rvalue reference 인 객체를 가지고 있는지 이해하지 못합니다. 유일한 문제는 누락 된 동등한 연산자였습니다.

#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

와우! 당신의 해결책에 대해 너무 감사드립니다. 이것은 절망적이었습니다. –

+0

귀하의 솔루션을 테스트하면 효과가 있습니다. 나는 operator = default가 일치하는 복사 생성자를 사용하기를 정말로 바란다. 아마도'자동차 개념'과 함께 할 것인가? –

+1

당신은 아마 저보다 그것에 대해 더 많이 알고 있습니다. 나는 오류 메시지 기수가되어 이것을 해결했다. – Potatoswatter

관련 문제