dimanche 27 août 2017

Defeated by vector

Coming back to C++ after many years; trying to catch up to C++11 & 14. I've read about rvalues and move semantics. I thought I understood the concept. Apparently not. I've looked at dozens of examples. But I simply can't get my code to compile. I must be missing something obvious in the examples. I always get the error about the copy ctor being deleted because of unique_ptr having a user-declared move ctor. There's obviously something I'm missing about the concept, but I can't figure out what it is. Here's the code, stripped down to its essence:

#include <utility>
#include <vector>

int main(int, char*[]) {
  auto oneInt{std::make_unique<int>(0)};
  auto someInts{std::vector<std::unique_ptr<int>>{std::move(oneInt)}};

  return 0;
}

What am I doing wrong?

Edit: Here's the error from this particular code. Note that I've tried every variation on the code that I can think of, with varying results, but the basic problem is always the same: copy ctor deleted because unique_ptr has a user-declared move ctor.

/Applications/http://ift.tt/2wA3gKI: error: call to implicitly-deleted copy constructor of
      'std::__1::unique_ptr<int, std::__1::default_delete<int> >'
            ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
                              ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/http://ift.tt/2wfSxTn: note: in instantiation of function template
      specialization 'std::__1::allocator<std::__1::unique_ptr<int, std::__1::default_delete<int> > >::construct<std::__1::unique_ptr<int, std::__1::default_delete<int> >,
      const std::__1::unique_ptr<int, std::__1::default_delete<int> > &>' requested here
            {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
                 ^
/Applications/http://ift.tt/2wAef6Q: note: in instantiation of function template
      specialization 'std::__1::allocator_traits<std::__1::allocator<std::__1::unique_ptr<int, std::__1::default_delete<int> > > >::__construct<std::__1::unique_ptr<int,
      std::__1::default_delete<int> >, const std::__1::unique_ptr<int, std::__1::default_delete<int> > &>' requested here
            {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
             ^
/Applications/http://ift.tt/2wfCFjF: note: in instantiation of function template
      specialization 'std::__1::allocator_traits<std::__1::allocator<std::__1::unique_ptr<int, std::__1::default_delete<int> > > >::construct<std::__1::unique_ptr<int,
      std::__1::default_delete<int> >, const std::__1::unique_ptr<int, std::__1::default_delete<int> > &>' requested here
                construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
                ^
/Applications/http://ift.tt/2wAcmHk: note: in instantiation of function template
      specialization 'std::__1::allocator_traits<std::__1::allocator<std::__1::unique_ptr<int, std::__1::default_delete<int> > > >::__construct_range_forward<const
      std::__1::unique_ptr<int, std::__1::default_delete<int> > *, std::__1::unique_ptr<int, std::__1::default_delete<int> > *>' requested here
    __alloc_traits::__construct_range_forward(__a, __first, __last, this->__end_);
                    ^
/Applications/http://ift.tt/2wfARXJ: note: in instantiation of function template
      specialization 'std::__1::vector<std::__1::unique_ptr<int, std::__1::default_delete<int> >, std::__1::allocator<std::__1::unique_ptr<int, std::__1::default_delete<int> >
      > >::__construct_at_end<const std::__1::unique_ptr<int, std::__1::default_delete<int> > *>' requested here
        __construct_at_end(__il.begin(), __il.end(), __il.size());
        ^
virtual.cpp:6:21: note: in instantiation of member function 'std::__1::vector<std::__1::unique_ptr<int, std::__1::default_delete<int> >,
      std::__1::allocator<std::__1::unique_ptr<int, std::__1::default_delete<int> > > >::vector' requested here
      auto someInts{std::vector<std::unique_ptr<int>>{std::move(oneInt)}};
                    ^
/Applications/http://ift.tt/2wzXx7V: note: copy constructor is implicitly deleted because
      'unique_ptr<int, std::__1::default_delete<int> >' has a user-declared move constructor
    _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT

Aucun commentaire:

Enregistrer un commentaire