为何以上代码在vs2020中可以编译通过,可运行,在gcc7.3.0下编译报错?
template<class T, class...ReqContextT>
class AsyncRequestSchedulerT2 final {
public:
void start(std::unique_ptr<MyClass1> c, ReqContextT&& ...reqContext)
{
using AsyncRequestScheduler = AsyncRequestSchedulerT2<T, ReqContextT...>;
if (c != nullptr) {
std::vector<std::thread> threads(c->count());
for (std::size_t pos = 1; pos < c->count(); pos++) {
threads[pos - 1] = std::thread(&AsyncRequestScheduler::processRequest, *this, std::forward<ReqContextT>(reqContext)...);
}
for (std::size_t pos = 0; pos < threads.size(); pos++) {
threads[pos].join();
}
}
processRequest(std::forward<ReqContextT>(reqContext)...);
}
void processRequest(ReqContextT&& ...reqContext)
{
std::cout << "processRequest" << std::endl;
}
};
gcc编译错误
from AsyncRequestSchedulerTest2.cpp:12:
/usr/include/c++/7.3.0/thread: In instantiation of ‘struct std::thread::_Invoker<std::tuple<void (MyAsyncRequestSchedulerT<int, AsyncContext&>::*)(AsyncContext&), MyAsyncRequestSchedulerT<int, AsyncContext&>, AsyncContext> >’:
/usr/include/c++/7.3.0/thread:127:22: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (MyAsyncRequestSchedulerT<int, AsyncContext&>::*)(AsyncContext&); _Args = {MyAsyncRequestSchedulerT<int, AsyncContext&>&, AsyncContext&}]’
test/dt_tests/AsyncRequestSchedulerTest2.cpp:102:41: required from ‘void MyAsyncRequestSchedulerT<T, ReqContextT>::start(std::unique_ptr<SchedulePolicy>, ReqContextT&& ...) [with T = int; ReqContextT = {AsyncContext&}]’
test/dt_tests/AsyncRequestSchedulerTest2.cpp:120:61: required from here
/usr/include/c++/7.3.0/thread:240:2: error: no matching function for call to ‘std::thread::_Invoker<std::tuple<void (MyAsyncRequestSchedulerT<int, AsyncContext&>::*)(AsyncContext&), MyAsyncRequestSchedulerT<int, AsyncContext&>, AsyncContext> >::_M_invoke(std::thread::_Invoker<std::tuple<void (MyAsyncRequestSchedulerT<int, AsyncContext&>::*)(AsyncContext&), MyAsyncRequestSchedulerT<int, AsyncContext&>, AsyncContext> >::_Indices)’
operator()()
^~~~~~~~
/usr/include/c++/7.3.0/thread:231:4: note: candidate: template<long unsigned int ..._Ind> decltype (std::__invoke((_S_declval<_Ind>)()...)) std::thread::_Invoker<_Tuple>::_M_invoke(std::_Index_tuple<_Ind ...>) [with long unsigned int ..._Ind = {_Ind ...}; _Tuple = std::tuple<void (MyAsyncRequestSchedulerT<int, AsyncContext&>::*)(AsyncContext&), MyAsyncRequestSchedulerT<int, AsyncContext&>, AsyncContext>]
_M_invoke(_Index_tuple<_Ind...>)
^~~~~~~~~
/usr/include/c++/7.3.0/thread:231:4: note: template argument deduction/substitution failed:
/usr/include/c++/7.3.0/thread: In substitution of ‘template<long unsigned int ..._Ind> decltype (std::__invoke(_S_declval<_Ind>()...)) std::thread::_Invoker<std::tuple<void (MyAsyncRequestSchedulerT<int, AsyncContext&>::*)(AsyncContext&), MyAsyncRequestSchedulerT<int, AsyncContext&>, AsyncContext> >::_M_invoke<_Ind ...>(std::_Index_tuple<_Ind1 ...>) [with long unsigned int ..._Ind = {0, 1, 2}]’:
/usr/include/c++/7.3.0/thread:240:2: required from ‘struct std::thread::_Invoker<std::tuple<void (MyAsyncRequestSchedulerT<int, AsyncContext&>::*)(AsyncContext&), MyAsyncRequestSchedulerT<int, AsyncContext&>, AsyncContext> >’
/usr/include/c++/7.3.0/thread:127:22: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (MyAsyncRequestSchedulerT<int, AsyncContext&>::*)(AsyncContext&); _Args = {MyAsyncRequestSchedulerT<int, AsyncContext&>&, AsyncContext&}]’
test/dt_tests/AsyncRequestSchedulerTest2.cpp:102:41: required from ‘void MyAsyncRequestSchedulerT<T, ReqContextT>::start(std::unique_ptr<SchedulePolicy>, ReqContextT&& ...) [with T = int; ReqContextT = {AsyncContext&}]’
test/dt_tests/AsyncRequestSchedulerTest2.cpp:120:61: required from here
/usr/include/c++/7.3.0/thread:233:29: error: no matching function for call to ‘__invoke(std::__tuple_element_t<0, std::tuple<void (MyAsyncRequestSchedulerT<int, AsyncContext&>::*)(AsyncContext&), MyAsyncRequestSchedulerT<int, AsyncContext&>, AsyncContext> >, std::__tuple_element_t<1, std::tuple<void (MyAsyncRequestSchedulerT<int, AsyncContext&>::*)(AsyncContext&), MyAsyncRequestSchedulerT<int, AsyncContext&>, AsyncContext> >, std::__tuple_element_t<2, std::tuple<void (MyAsyncRequestSchedulerT<int, AsyncContext&>::*)(AsyncContext&), MyAsyncRequestSchedulerT<int, AsyncContext&>, AsyncContext> >)’
-> decltype(std::__invoke(_S_declval<_Ind>()...))
~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7.3.0/tuple:41:0,
from /usr/include/c++/7.3.0/bits/unique_ptr.h:37,
from /usr/include/c++/7.3.0/memory:80,
from /googletest/googletest/include/gtest/gtest.h:54,
from test/dt_tests/AsyncRequestSchedulerTest2.cpp:5:
/usr/include/c++/7.3.0/bits/invoke.h:89:5: note: candidate: template<class _Callable, class ... _Args> constexpr typename std::__invoke_result<_Functor, _ArgTypes>::type std::__invoke(_Callable&&, _Args&& ...)
__invoke(_Callable&& __fn, _Args&&... __args)
^~~~~~~~
/usr/include/c++/7.3.0/bits/invoke.h:89:5: note: template argument deduction/substitution failed:
/usr/include/c++/7.3.0/bits/invoke.h: In substitution of ‘template<class _Callable, class ... _Args> constexpr typename std::__invoke_result<_Functor, _ArgTypes>::type std::__invoke(_Callable&&, _Args&& ...) [with _Callable = void (MyAsyncRequestSchedulerT<int, AsyncContext&>::*)(AsyncContext&); _Args = {MyAsyncRequestSchedulerT<int, AsyncContext&>, AsyncContext}]’:
/usr/include/c++/7.3.0/thread:233:29: required by substitution of ‘template<long unsigned int ..._Ind> decltype (std::__invoke(_S_declval<_Ind>()...)) std::thread::_Invoker<std::tuple<void (MyAsyncRequestSchedulerT<int, AsyncContext&>::*)(AsyncContext&), MyAsyncRequestSchedulerT<int, AsyncContext&>, AsyncContext> >::_M_invoke<_Ind ...>(std::_Index_tuple<_Ind1 ...>) [with long unsigned int ..._Ind = {0, 1, 2}]’
/usr/include/c++/7.3.0/thread:240:2: required from ‘struct std::thread::_Invoker<std::tuple<void (MyAsyncRequestSchedulerT<int, AsyncContext&>::*)(AsyncContext&), MyAsyncRequestSchedulerT<int, AsyncContext&>, AsyncContext> >’
/usr/include/c++/7.3.0/thread:127:22: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (MyAsyncRequestSchedulerT<int, AsyncContext&>::*)(AsyncContext&); _Args = {MyAsyncRequestSchedulerT<int, AsyncContext&>&, AsyncContext&}]’
test/dt_tests/AsyncRequestSchedulerTest2.cpp:102:41: required from ‘void MyAsyncRequestSchedulerT<T, ReqContextT>::start(std::unique_ptr<SchedulePolicy>, ReqContextT&& ...) [with T = int; ReqContextT = {AsyncContext&}]’
test/dt_tests/AsyncRequestSchedulerTest2.cpp:120:61: required from here
/usr/include/c++/7.3.0/bits/invoke.h:89:5: error: no type named ‘type’ in ‘struct std::__invoke_result<void (MyAsyncRequestSchedulerT<int, AsyncContext&>::*)(AsyncContext&), MyAsyncRequestSchedulerT<int, AsyncContext&>, AsyncContext>’
make[2]: *** [test/dt_tests/CMakeFiles/AsyncRequestSchedulerTest2.dir/build.make:63: test/dt_tests/CMakeFiles/AsyncRequestSchedulerTest2.dir/AsyncRequestSchedulerTest2.cpp.o] Error 1
make[2]: Leaving directory 'build'
make[1]: *** [CMakeFiles/Makefile2:4765: test/dt_tests/CMakeFiles/AsyncRequestSchedulerTest2.dir/all] Error 2
make[1]: Leaving directory 'build'
make: *** [Makefile:84: all] Error 2