2015-07-22 153 views
8

我看到一些錯誤通過std::vector< std::unique_ptr<T> >std::move。再現該問題的代碼是這樣的:錯誤`std :: vector <std :: unique_ptr < T >>`

#include <memory> // for std::unique_ptr 
#include <utility> // for std::move 
#include <vector> // for std::vector 

struct bar {}; 
using vtype = std::vector<std::unique_ptr<bar>>; 

struct foo 
{ 
    foo(vtype v) : _v(std::move(v)) { } 
private: 
    vtype _v; 
}; 

vtype getVector() 
{ 
    return { std::move(std::unique_ptr<bar>(new bar())) }; 
};  

int main() 
{ 
foo f(std::move(getVector())); 
}; 

隨着鐺3.4,此代碼產生這樣的錯誤:

$ clang++ -std=c++11 test.cpp -o xtest 
In file included from test.cpp:1: 
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/memory:64: 
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_construct.h:75:38: error: call to deleted constructor of 
     'std::unique_ptr<bar, std::default_delete<bar> >' 
    { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); } 
            ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_uninitialized.h:75:8: note: in instantiation of function template specialization 
     'std::_Construct<std::unique_ptr<bar, std::default_delete<bar> >, const std::unique_ptr<bar, std::default_delete<bar> > &>' requested here 
       std::_Construct(std::__addressof(*__cur), *__first); 
        ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_uninitialized.h:117:2: note: in instantiation of function template specialization 
     'std::__uninitialized_copy<false>::__uninit_copy<const std::unique_ptr<bar, std::default_delete<bar> > *, std::unique_ptr<bar, std::default_delete<bar> > *>' 
     requested here 
     __uninit_copy(__first, __last, __result); 
     ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_uninitialized.h:258:19: note: in instantiation of function template specialization 
     'std::uninitialized_copy<const std::unique_ptr<bar, std::default_delete<bar> > *, std::unique_ptr<bar, std::default_delete<bar> > *>' requested here 
    { return std::uninitialized_copy(__first, __last, __result); } 
       ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_vector.h:1204:11: note: in instantiation of function template specialization 
     'std::__uninitialized_copy_a<const std::unique_ptr<bar, std::default_delete<bar> > *, std::unique_ptr<bar, std::default_delete<bar> > *, std::unique_ptr<bar, 
     std::default_delete<bar> > >' requested here 
      std::__uninitialized_copy_a(__first, __last, 
       ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_vector.h:368:2: note: in instantiation of function template specialization 
     'std::vector<std::unique_ptr<bar, std::default_delete<bar> >, std::allocator<std::unique_ptr<bar, std::default_delete<bar> > > >::_M_range_initialize<const 
     std::unique_ptr<bar, std::default_delete<bar> > *>' requested here 
     _M_range_initialize(__l.begin(), __l.end(), 
     ^
test.cpp:17:12: note: in instantiation of member function 'std::vector<std::unique_ptr<bar, std::default_delete<bar> >, std::allocator<std::unique_ptr<bar, 
     std::default_delete<bar> > > >::vector' requested here 
    return { std::move(std::unique_ptr<bar>(new bar())) }; 
     ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/unique_ptr.h:273:7: note: function has been explicitly marked deleted here 
     unique_ptr(const unique_ptr&) = delete; 
    ^
1 error generated. 

的情況下似乎不具有任何與克++ 4.8更好:

$ g++-4.8 -std=c++11 test.cpp -o xtest 
In file included from /usr/include/c++/4.8/memory:64:0, 
       from test.cpp:1: 
/usr/include/c++/4.8/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::unique_ptr<bar>; _Args = {const std::unique_ptr<bar, std::default_delete<bar> >&}]’: 
/usr/include/c++/4.8/bits/stl_uninitialized.h:75:53: required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const std::unique_ptr<bar>*; _ForwardIterator = std::unique_ptr<bar>*; bool _TrivialValueTypes = false]’ 
/usr/include/c++/4.8/bits/stl_uninitialized.h:117:41: required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const std::unique_ptr<bar>*; _ForwardIterator = std::unique_ptr<bar>*]’ 
/usr/include/c++/4.8/bits/stl_uninitialized.h:258:63: required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = const std::unique_ptr<bar>*; _ForwardIterator = std::unique_ptr<bar>*; _Tp = std::unique_ptr<bar>]’ 
/usr/include/c++/4.8/bits/stl_vector.h:1206:27: required from ‘void std::vector<_Tp, _Alloc>::_M_range_initialize(_ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = const std::unique_ptr<bar>*; _Tp = std::unique_ptr<bar>; _Alloc = std::allocator<std::unique_ptr<bar> >]’ 
/usr/include/c++/4.8/bits/stl_vector.h:369:36: required from ‘std::vector<_Tp, _Alloc>::vector(std::initializer_list<_Tp>, const allocator_type&) [with _Tp = std::unique_ptr<bar>; _Alloc = std::allocator<std::unique_ptr<bar> >; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::unique_ptr<bar> >]’ 
test.cpp:17:59: required from here 
/usr/include/c++/4.8/bits/stl_construct.h:75:7: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = bar; _Dp = std::default_delete<bar>]’ 
    { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); } 
    ^
In file included from /usr/include/c++/4.8/memory:81:0, 
       from test.cpp:1: 
/usr/include/c++/4.8/bits/unique_ptr.h:273:7: error: declared here 
     unique_ptr(const unique_ptr&) = delete; 
    ^

根據this answer和評論,這不應該發生在這些編譯器上,但我沒有完全相同:我試圖用初始化列表初始化向量。

任何想法爲了這段代碼正確地構建需要發生什麼?

+1

可以輕鬆地與一個不可複製的'bar' HTTP轉載/coliru.stacked-crooked.com/a/f8344e87a2d4374e – 0x499602D2

回答

8

的的在return語句支撐,初始化列表的到std::vector<T>構造函數的參數initializer_list<T>通話中getVector

return { std::move(std::unique_ptr<bar>(new bar())) }; 

結果使用。即使您正在移動unique_ptr,initializer_listonly allows const access to its elements,因此vector將嘗試複製unique_ptr,導致您看到的錯誤。

可以訴諸構建vector

vtype getVector() 
{ 
    vtype v; 
    v.push_back(std::unique_ptr<bar>(new bar())); 
    return v; 
} 

Live demo


爲了好奇的一個更詳細的方式修復錯誤,所以可以從陣列構造vector僅移動對象,但您需要通過std::move_iterator移動元素。

vtype getVector() 
{ 
    std::unique_ptr<bar> arr[] = {std::unique_ptr<bar>(new bar())}; 
    return {std::make_move_iterator(std::begin(arr)), 
      std::make_move_iterator(std::end(arr))}; 
} 
+2

@ 0x499602D2'push_back'返回'void' ...? –

+0

@ T.C。你是對的:) – 0x499602D2

+0

我寧願'v.emplace_back(新酒吧());'或'v.push_back(std :: make_unique ());' –

-1

你的vtype是一個唯一指針向量,你返回一個唯一的指針。

,因爲你是在做你不需要返回舉動 看看這個代碼構造FOO移動:/:

#include <memory> // for std::unique_ptr 
#include <utility> // for std::move 
#include <vector> // for std::vector 

struct bar {}; 
using vtype = /*std::vector<*/std::unique_ptr<bar>/*>*/; 

struct foo 
{ 
    foo(vtype v) : _v(std::move(v)) { } 
private: 
    vtype _v; 
}; 

vtype getVector() 
{ 
    return /*{ std::move(*/ std::unique_ptr<bar>(new bar()) /*) }*/; 
};  

int main() 
{ 
foo f(std::move(getVector())); 
}; 
相關問題