我可以列出初始化一个只移动types的向量吗?

如果我通过我的GCC 4.7快照传递下面的代码,它会尝试将unique_ptr复制到vector中。

 #include <vector> #include <memory> int main() { using move_only = std::unique_ptr<int>; std::vector<move_only> v { move_only(), move_only(), move_only() }; } 

显然,不能工作,因为std::unique_ptr是不可复制的:

错误:使用已删除的函数std :: unique_ptr <_Tp,_Dp> :: unique_ptr(const std :: unique_ptr <_Tp,_Dp>&)[with _Tp = int; _Dp = std :: default_delete; std :: unique_ptr <_Tp,_Dp> = std :: unique_ptr]'

试图复制初始化列表中的指针是否正确?

18.9中<initializer_list>的概要说明了初始化列表的元素总是通过const-reference传递。 不幸的是,在当前语言版本的初始化列表元素中似乎没有使用移动语义的方法。

具体来说,我们有:

 typedef const E& reference; typedef const E& const_reference; typedef const E* iterator; typedef const E* const_iterator; const E* begin() const noexcept; // first element const E* end() const noexcept; // one past the last element 

编辑:由于@Johannes似乎不想发布最好的解决scheme作为答案,我只是这样做。

 #include <iterator> #include <vector> #include <memory> int main(){ using move_only = std::unique_ptr<int>; move_only init[] = { move_only(), move_only(), move_only() }; std::vector<move_only> v{std::make_move_iterator(std::begin(init)), std::make_move_iterator(std::end(init))}; } 

std::make_move_iterator返回的迭代器将在被解引用时移动指向元素。


原始答案:我们要在这里使用一个小帮手types:

 #include <utility> #include <type_traits> template<class T> struct rref_wrapper { // CAUTION - very volatile, use with care explicit rref_wrapper(T&& v) : _val(std::move(v)) {} explicit operator T() const{ return T{ std::move(_val) }; } private: T&& _val; }; // only usable on temporaries template<class T> typename std::enable_if< !std::is_lvalue_reference<T>::value, rref_wrapper<T> >::type rref(T&& v){ return rref_wrapper<T>(std::move(v)); } // lvalue reference can go away template<class T> void rref(T&) = delete; 

可悲的是,这里直接的代码将不起作用:

 std::vector<move_only> v{ rref(move_only()), rref(move_only()), rref(move_only()) }; 

由于标准,无论出于何种原因,没有定义一个像这样的转换拷贝构造函数:

 // in class initializer_list template<class U> initializer_list(initializer_list<U> const& other); 

由brace-init-list( {...} )创build的initializer_list<rref_wrapper<move_only>>将不会转换为vector<move_only>采用的initializer_list<move_only> 。 所以我们需要两步初始化:

 std::initializer_list<rref_wrapper<move_only>> il{ rref(move_only()), rref(move_only()), rref(move_only()) }; std::vector<move_only> v(il.begin(), il.end()); 

正如其他答案中所提到的, std::initializer_list的行为是通过值保存对象,不允许移出,所以这是不可能的。 下面是一个可能的解决方法,使用一个函数调用,其中初始值设定项是可变参数:

 #include <vector> #include <memory> struct Foo { std::unique_ptr<int> u; int x; Foo(int x = 0): x(x) {} }; template<typename V> // recursion-ender void multi_emplace(std::vector<V> &vec) {} template<typename V, typename T1, typename... Types> void multi_emplace(std::vector<V> &vec, T1&& t1, Types&&... args) { vec.emplace_back( std::move(t1) ); multi_emplace(vec, args...); } int main() { std::vector<Foo> foos; multi_emplace(foos, 1, 2, 3, 4, 5); multi_emplace(foos, Foo{}, Foo{}); } 

不幸的是multi_emplace(foos, {}); 因为它不能推导{}的types,所以对于被默认构造的对象,你必须重复类名。 (或使用vector::resize

使用Johannes Schaub的std::make_move_iterator()std::experimental::make_array()的技巧,你可以使用一个辅助函数:

 #include <memory> #include <type_traits> #include <vector> #include <experimental/array> struct X {}; template<class T, std::size_t N> auto make_vector( std::array<T,N>&& a ) -> std::vector<T> { return { std::make_move_iterator(std::begin(a)), std::make_move_iterator(std::end(a)) }; } template<class... T> auto make_vector( T&& ... t ) -> std::vector<typename std::common_type<T...>::type> { return make_vector( std::experimental::make_array( std::forward<T>(t)... ) ); } int main() { using UX = std::unique_ptr<X>; const auto a = std::experimental::make_array( UX{}, UX{}, UX{} ); // Ok const auto v0 = make_vector( UX{}, UX{}, UX{} ); // Ok //const auto v1 = std::vector< UX >{ UX{}, UX{}, UX{} }; // !! Error !! } 

Coliru上看到它。

也许有人可以利用std::make_array()make_vector()来让make_vector()直接做它的事情,但我没有看到(更准确地说,我尝试了我认为应该工作,失败,并继续前进)。 在任何情况下,编译器都应该能够将数组内联到vector变换上,就像Clang在GodBolt上使用O2 一样

正如已经指出的那样,用初始化列表初始化移动types的vector是不可能的。 最初由@Johannes提出的解决scheme工作正常,但我有另一个想法…如果我们不创build一个临时数组,然后将元素从那里移动到vector,但使用放置new的初始化此数组已经取代vector的内存块?

这是我的函数初始化unique_ptr的向量使用参数包:

 #include <iostream> #include <vector> #include <make_unique.h> /// @see http://stackoverflow.com/questions/7038357/make-unique-and-perfect-forwarding template <typename T, typename... Items> inline std::vector<std::unique_ptr<T>> make_vector_of_unique(Items&&... items) { typedef std::unique_ptr<T> value_type; // Allocate memory for all items std::vector<value_type> result(sizeof...(Items)); // Initialize the array in place of allocated memory new (result.data()) value_type[sizeof...(Items)] { make_unique<typename std::remove_reference<Items>::type>(std::forward<Items>(items))... }; return result; } int main(int, char**) { auto testVector = make_vector_of_unique<int>(1,2,3); for (auto const &item : testVector) { std::cout << *item << std::endl; } }