Why it is not possible to use an initializer_list to initialize a vector of unique_ptr's? [duplicate]

一笑奈何 提交于 2021-02-16 08:55:29

问题


I'm wondering why initializer_list doesn't work with unique_ptr:

std::vector<std::unique_ptr<int>> vptr = {std::make_unique<int>(1), std::make_unique<int>(2)};

do not compile.

However:

std::vector<std::unique_ptr<int>> vptr(2);
vptr[0] =std::make_unique<int>(1);
vptr[1] =std::make_unique<int>(2);

compile.

and

std::vector<int*>vint={new int{1},new int{2}};

or

 std::vector<std::shared_ptr<int>> vptr= {std::make_shared<int>(1), std::make_shared<int>(2)};

compile.

The unique_ptr variant is giving this error message in clang:

In file included from main.cpp:2:
In file included from /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/vector:62:
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/bits/stl_construct.h:75:38: error: call to deleted constructor of 'std::unique_ptr<int, std::default_delete<int> >'
    { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
                                     ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/bits/stl_uninitialized.h:75:8: note: in instantiation of function template specialization 'std::_Construct<std::unique_ptr<int, std::default_delete<int> >, const std::unique_ptr<int, std::default_delete<int> > &>' requested here
                std::_Construct(std::__addressof(*__cur), *__first);
                     ^
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/bits/stl_uninitialized.h:125:2: note: in instantiation of function template specialization 'std::__uninitialized_copy<false>::__uninit_copy<const std::unique_ptr<int, std::default_delete<int> > *, std::unique_ptr<int, std::default_delete<int> > *>' requested here
        __uninit_copy(__first, __last, __result);
        ^
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/bits/stl_uninitialized.h:278:19: note: in instantiation of function template specialization 'std::uninitialized_copy<const std::unique_ptr<int, std::default_delete<int> > *, std::unique_ptr<int, std::default_delete<int> > *>' requested here
    { return std::uninitialized_copy(__first, __last, __result); }
                  ^
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/bits/stl_vector.h:1284:11: note: in instantiation of function template specialization 'std::__uninitialized_copy_a<const std::unique_ptr<int, std::default_delete<int> > *, std::unique_ptr<int, std::default_delete<int> > *, std::unique_ptr<int, std::default_delete<int> > >' requested here
            std::__uninitialized_copy_a(__first, __last,
                 ^
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/bits/stl_vector.h:377:2: note: in instantiation of function template specialization 'std::vector<std::unique_ptr<int, std::default_delete<int> >, std::allocator<std::unique_ptr<int, std::default_delete<int> > > >::_M_range_initialize<const std::unique_ptr<int, std::default_delete<int> > *>' requested here
        _M_range_initialize(__l.begin(), __l.end(),
        ^
main.cpp:10:42: note: in instantiation of member function 'std::vector<std::unique_ptr<int, std::default_delete<int> >, std::allocator<std::unique_ptr<int, std::default_delete<int> > > >::vector' requested here
std::vector<std::unique_ptr<int>> vptr = {std::make_unique<int>(1), std::make_unique<int>(2)};
                                         ^
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/bits/unique_ptr.h:356:7: note: 'unique_ptr' has been explicitly marked deleted here
      unique_ptr(const unique_ptr&) = delete;
      ^
1 error generated.

回答1:


The constructor for vector<T> taking an initializer_list<T> copies the elements from the initializer list into the vector. This does not work when you're dealing with an uncopyable type: the design of unique_ptr<T> prevents copies from being made. It does allow moving, which is why assignment works: make_unique returns an rvalue, for which the compiler knows it's safe to move.

You'd need some sort of constructor taking an initializer_list<T &&>, but C++ doesn't have such a constructor (nor indeed, as dyp pointed out, does the type initializer_list<T &&> even function).



来源:https://stackoverflow.com/questions/26389595/why-it-is-not-possible-to-use-an-initializer-list-to-initialize-a-vector-of-uniq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!