3

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.
Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160
adel
  • 95
  • 5

1 Answers1

5

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).

  • Is there a reason why `initializer_list` is missing? Given that the assignment works, I feel it would be only consequent if it was available. – davidhigh Oct 15 '14 at 18:43
  • 5
    I don't think an `initializer_list` can exist: it cannot be implemented as an array (see 8.5.4/5). – dyp Oct 15 '14 at 18:49
  • @dyp Quite right, thanks, and worth including as a note. –  Oct 16 '14 at 06:43