silent_coder
silent_coder

Reputation: 6532

strange error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr when no pointers really created

I have a class, which looks like that:

    template<typename T>
    using VectorPtr=std::vector<std::unique_ptr<T>>;

    template<typename T>
    using VectorRawPtr=std::vector<T*>;

    class ItemsSet{ // <-- Compiler say this line contans an error 0_o ?
    public:
          ItemsSet(VectorPtr<Item>& items);  

          ~ItemsSet() = default;

           VectorRawPtr<Item> GetItems();

           VectorRawPtr<Item> GetSuitableItemsForPeriod(const IPeriod &period);

           double CalculateTotal();
    private:
       VectorPtr<Item> _items;
    };

constructor looks like:

ItemsSet::ItemsSet(VectorPtr<Item> & items) {
     for(auto &itm: items){
        _items.emplace_back(std::move(itm));
     }
}

however this code isn't compiled and failed with error:

/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_construct.h: In instantiation of 'void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::unique_ptr<Item, std::default_delete<Item> >; _Args = {const std::unique_ptr<Item, std::default_delete<Item> >&}]':
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_uninitialized.h:75:18:   required from 'static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<Item, std::default_delete<Item> >*, std::vector<std::unique_ptr<Item, std::default_delete<Item> >, std::allocator<std::unique_ptr<Item, std::default_delete<Item> > > > >; _ForwardIterator = std::unique_ptr<Item, std::default_delete<Item> >*; bool _TrivialValueTypes = false]'
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_uninitialized.h:126:15:   required from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<Item, std::default_delete<Item> >*, std::vector<std::unique_ptr<Item, std::default_delete<Item> >, std::allocator<std::unique_ptr<Item, std::default_delete<Item> > > > >; _ForwardIterator = std::unique_ptr<Item, std::default_delete<Item> >*]'
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_uninitialized.h:281:37:   required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<Item, std::default_delete<Item> >*, std::vector<std::unique_ptr<Item, std::default_delete<Item> >, std::allocator<std::unique_ptr<Item, std::default_delete<Item> > > > >; _ForwardIterator = std::unique_ptr<Item, std::default_delete<Item> >*; _Tp = std::unique_ptr<Item, std::default_delete<Item> >]'
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_vector.h:322:31:   required from 'std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::unique_ptr<Item, std::default_delete<Item> >; _Alloc = std::allocator<std::unique_ptr<Item, std::default_delete<Item> > >]'
/cygdrive/d/code/itemSet.h:4:19:   required from here
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/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 = Item; _Dp = std::default_delete<Item>]'
     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }

Could anyone explain me what I'm doing wrong and how could I fix my problem?

Upvotes: 17

Views: 29080

Answers (3)

Ph0en1x
Ph0en1x

Reputation: 10087

I'm pretty sure that actual problem is an implicit copy constructor ither for ItemsSet or Item. Because you using unique_ptr's which can't really be copied, copy constructor can't be generated properly. Try to explicitly delete copy constructors and find the place where they used and change those place to move declaration for example, or use shared pointers.

Upvotes: 20

Andy
Andy

Reputation: 30418

I don't know if this will fix it or not, but you might try moving the constructor parameter directly into _items instead of moving each individual member into it:

 ItemsSet::ItemsSet(VectorPtr<Item>&& items)
 : _items(std::move(items))
 {
 }

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385385

This isn't the actual code that produces the error (your line numbers don't match, and neither do the errors; you should present an actual testcase here), but we can still see the problem.

unique_ptrs cannot be copied (they're "unique"!), yet by copy-initialising _items from a whole vector of them, you're attempting to copy them all. You can't do that.

You could move the constructor argument into _items instead.

Upvotes: 4

Related Questions