std::vector<std::thread> result type must be constructible from value type of input range

问题描述:

In file included from /usr/include/c++/11/memory:66,
                 from /tmp/tmp.Ok1lo5h73E/Thread/BusThreadPool/BusThreadPool.h:9,
                 from /tmp/tmp.Ok1lo5h73E/Thread/BusThreadPool/BusThreadPool.cpp:4:
/usr/include/c++/11/bits/stl_uninitialized.h: In instantiation of ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::thread*, std::vector<std::thread> >; _ForwardIterator = std::thread*]’:
/usr/include/c++/11/bits/stl_uninitialized.h:333:37:   required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::thread*, std::vector<std::thread> >; _ForwardIterator = std::thread*; _Tp = std::thread]’
/usr/include/c++/11/bits/stl_vector.h:558:31:   required from ‘std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::thread; _Alloc = std::allocator<std::thread>]’
/tmp/tmp.Ok1lo5h73E/Thread/BusThreadPool/../../PipeManager/CustomerExecute.h:15:7:   required from here
/usr/include/c++/11/bits/stl_uninitialized.h:138:72: error: static assertion failed: result type must be constructible from value type of input range
  138 |       static_assert(is_constructible<_ValueType2, decltype(*__first)>::value,
      |                                                                        ^~~~~
/usr/include/c++/11/bits/stl_uninitialized.h:138:72: note: ‘std::integral_constant<bool, false>::value’ evaluates to false
In file included from /usr/include/c++/11/vector:66,
                 from /tmp/tmp.Ok1lo5h73E/Thread/BusThreadPool/../../PublicClass/Bus.h:10,
                 from /tmp/tmp.Ok1lo5h73E/Thread/BusThreadPool/BusThreadExecute.h:14,
                 from /tmp/tmp.Ok1lo5h73E/Thread/BusThreadPool/BusThreadExecute.cpp:5:
/usr/include/c++/11/bits/stl_uninitialized.h: In instantiation of ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::thread*, std::vector<std::thread> >; _ForwardIterator = std::thread*]’:
/usr/include/c++/11/bits/stl_uninitialized.h:333:37:   required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::thread*, std::vector<std::thread> >; _ForwardIterator = std::thread*; _Tp = std::thread]’
/usr/include/c++/11/bits/stl_vector.h:558:31:   required from ‘std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::thread; _Alloc = std::allocator<std::thread>]’
/tmp/tmp.Ok1lo5h73E/Thread/BusThreadPool/../../PipeManager/CustomerExecute.h:15:7:   required from here
/usr/include/c++/11/bits/stl_uninitialized.h:138:72: error: static assertion failed: result type must be constructible from value type of input range
  138 |       static_assert(is_constructible<_ValueType2, decltype(*__first)>::value,
      |                                                                        ^~~~~
/usr/include/c++/11/bits/stl_uninitialized.h:138:72: note: ‘std::integral_constant<bool, false>::value’ evaluates to false
gmake[3]: *** [Thread/CMakeFiles/Thread.dir/build.make:76: Thread/CMakeFiles/Thread.dir/BusThreadPool/BusThreadPool.cpp.o] Error 1
gmake[3]: *** Waiting for unfinished jobs....
gmake[3]: *** [Thread/CMakeFiles/Thread.dir/build.make:90: Thread/CMakeFiles/Thread.dir/BusThreadPool/BusThreadExecute.cpp.o] Error 1
gmake[2]: *** [CMakeFiles/Makefile2:497: Thread/CMakeFiles/Thread.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:295: CMakeFiles/SsuDataComputeDriver.dir/rule] Error 2
gmake: *** [Makefile:124: SsuDataComputeDriver] Error 2

问题原因:

当定义std::vector<std::thread> 却没有在std::vector内添加任何对象会产生上述静态断言错误,表明std::thread不是可平凡构造类型

解决方法:

当使用std::vector<std::thread>时一定要在构造函数中向容器内压入一些std::thread对象,或者干脆不要定义这种空对象

posted @ 2025-04-09 18:57  BlackSnow  阅读(171)  评论(0)    收藏  举报