samedi 29 mai 2021

c++ STL using vector

I defined a ThreadSafe List and I want to declear a vector to save some ThreadSafe list, but when I initialized the vector using resize, I got some error.

The SafeList I defined:

template<class T>
class SafeQueue{
public:
    SafeQueue() {}
    SafeQueue(const SafeQueue & sq) = delete;
    SafeQueue & operator = (const SafeQueue & sq) = delete;

private:
    std::queue<T> queue_;
    std::mutex mutex_;

And the Initialization code is:

for (auto & p : timer_) {
    // p type: std::vector<SafeContainer::SafeList<TimerNodePtr> >
    p.resize(SHIFT);
}

And the error information followed:

/usr/include/c++/9/bits/stl_uninitialized.h: In instantiation of ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<SafeContainer::SafeList<std::shared_ptr<Timer::TimerNode> >*>; _ForwardIterator = SafeContainer::SafeList<std::shared_ptr<Timer::TimerNode> >*]’:
/usr/include/c++/9/bits/stl_uninitialized.h:307:37:   required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::move_iterator<SafeContainer::SafeList<std::shared_ptr<Timer::TimerNode> >*>; _ForwardIterator = SafeContainer::SafeList<std::shared_ptr<Timer::TimerNode> >*; _Tp = SafeContainer::SafeList<std::shared_ptr<Timer::TimerNode> >]’
/usr/include/c++/9/bits/stl_uninitialized.h:329:2:   required from ‘_ForwardIterator std::__uninitialized_move_if_noexcept_a(_InputIterator, _InputIterator, _ForwardIterator, _Allocator&) [with _InputIterator = SafeContainer::SafeList<std::shared_ptr<Timer::TimerNode> >*; _ForwardIterator = SafeContainer::SafeList<std::shared_ptr<Timer::TimerNode> >*; _Allocator = std::allocator<SafeContainer::SafeList<std::shared_ptr<Timer::TimerNode> > >]’
/usr/include/c++/9/bits/vector.tcc:659:48:   required from ‘void std::vector<_Tp, _Alloc>::_M_default_append(std::vector<_Tp, _Alloc>::size_type) [with _Tp = SafeContainer::SafeList<std::shared_ptr<Timer::TimerNode> >; _Alloc = std::allocator<SafeContainer::SafeList<std::shared_ptr<Timer::TimerNode> > >; std::vector<_Tp, _Alloc>::size_type = long unsigned int]’
/usr/include/c++/9/bits/stl_vector.h:937:4:   required from ‘void std::vector<_Tp, _Alloc>::resize(std::vector<_Tp, _Alloc>::size_type) [with _Tp = SafeContainer::SafeList<std::shared_ptr<Timer::TimerNode> >; _Alloc = std::allocator<SafeContainer::SafeList<std::shared_ptr<Timer::TimerNode> > >; std::vector<_Tp, _Alloc>::size_type = long unsigned int]’
/home/ctrlz/workSpace/MyServer/src/Timer.cpp:8:23:   required from here
/usr/include/c++/9/bits/stl_uninitialized.h:127:72: error: static assertion failed: result type must be constructible from value type of input range
  127 |       static_assert(is_constructible<_ValueType2, decltype(*__first)>::value,

Can someone help me? Thanks!


minimal reproducible example as followed:

#include <vector>
#include <queue>
#include <mutex>
template<class T>
class SafeQueue{
public:
    SafeQueue() {}
    SafeQueue(const SafeQueue & sq) = delete;
    SafeQueue & operator = (const SafeQueue & sq) = delete;


private:
    std::queue<T> queue_;
    std::mutex mutex_;
};

int main() {
    std::vector<SafeQueue<int> > list;
    list.resize(5);
}

Aucun commentaire:

Enregistrer un commentaire