:: unique_ptr when no pointers are created I have a class that looks like th...">

Strange error: using remote function "std :: unique_ptr <_Tp, _Dp> :: unique_ptr when no pointers are created

I have a class that looks like this:

    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

as follows:

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

however, this code is not compiled and not with an 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)...); }

Can someone explain to me what I'm doing wrong and how can I fix my problem?

+4
source share
3 answers

, ither ItemsSet Item. unique_ptr, , . , , , , .

+6

, ( , , ), .

unique_ptr ( !), - _items . .

_items.

+1

I don't know if this will fix it or not, but you can try moving the constructor parameter directly to _itemsinstead of moving every single element into it:

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

Source: https://habr.com/ru/post/1663320/


All Articles