#include<iostream>
#include<vector>
#include<thread>
#include<functional>
using namespace std;
typedef function<float(int)> lambda_t;
template<typename ty,typename funtor,typename res>
void traverse(ty &container,funtor lamda,res & out)
{
int ind = 0;
for(auto it=container.begin();it!=container.end();it++)
{
out[ind++] = lamda(*it);
}
}
int main()
{
vector<int> inp;
for(int i=0;i<1000;i++)
inp.push_back(rand()%1000);
lambda_t lamda = [](int a){return (float)(a*2.5);};
vector<float> outp(1000);
thread t1(traverse<vector<int>,lambda_t,vector<float> >,inp,lamda,outp);
int ind=0;
for(auto i:outp) cout<<inp[ind++]<<" "<<i<<endl;
}
Above code is just for learning and I want to call the traverse function in a new thread. I think the issue is with the type of lambda parameter to traverse function and compiler says it cannot deduce type. Any help is appreciated. Also I'm new here so please let me know if I need to improve my question. Thanks
EDIT: Compiler g++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
Error message
In file included from teamplate_traverse.cpp:3:0:
/usr/include/c++/7/thread: In instantiation of ‘struct std::thread::_Invoker<std::tuple<void (*)(std::vector<int, std::allocator<int> >&, std::function<float(int)>, std::vector<float, std::allocator<float> >&), std::vector<int, std::allocator<int> >, std::function<float(int)>, std::vector<float, std::allocator<float> > > >’:
/usr/include/c++/7/thread:127:22: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(std::vector<int>&, std::function<float(int)>, std::vector<float>&); _Args = {std::vector<int, std::allocator<int> >&, std::function<float(int)>&, std::vector<float, std::allocator<float> >&}]’
teamplate_traverse.cpp:29:72: required from here
/usr/include/c++/7/thread:240:2: error: no matching function for call to ‘std::thread::_Invoker<std::tuple<void (*)(std::vector<int, std::allocator<int> >&, std::function<float(int)>, std::vector<float, std::allocator<float> >&), std::vector<int, std::allocator<int> >, std::function<float(int)>, std::vector<float, std::allocator<float> > > >::_M_invoke(std::thread::_Invoker<std::tuple<void (*)(std::vector<int, std::allocator<int> >&, std::function<float(int)>, std::vector<float, std::allocator<float> >&), std::vector<int, std::allocator<int> >, std::function<float(int)>, std::vector<float, std::allocator<float> > > >::_Indices)’
operator()()
^~~~~~~~
/usr/include/c++/7/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 (*)(std::vector<int, std::allocator<int> >&, std::function<float(int)>, std::vector<float, std::allocator<float> >&), std::vector<int, std::allocator<int> >, std::function<float(int)>, std::vector<float, std::allocator<float> > >]
_M_invoke(_Index_tuple<_Ind...>)
^~~~~~~~~
/usr/include/c++/7/thread:231:4: note: template argument deduction/substitution failed:
/usr/include/c++/7/thread: In substitution of ‘template<long unsigned int ..._Ind> decltype (std::__invoke(_S_declval<_Ind>()...)) std::thread::_Invoker<std::tuple<void (*)(std::vector<int, std::allocator<int> >&, std::function<float(int)>, std::vector<float, std::allocator<float> >&), std::vector<int, std::allocator<int> >, std::function<float(int)>, std::vector<float, std::allocator<float> > > >::_M_invoke<_Ind ...>(std::_Index_tuple<_Ind1 ...>) [with long unsigned int ..._Ind = {0, 1, 2, 3}]’:
/usr/include/c++/7/thread:240:2: required from ‘struct std::thread::_Invoker<std::tuple<void (*)(std::vector<int, std::allocator<int> >&, std::function<float(int)>, std::vector<float, std::allocator<float> >&), std::vector<int, std::allocator<int> >, std::function<float(int)>, std::vector<float, std::allocator<float> > > >’
/usr/include/c++/7/thread:127:22: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(std::vector<int>&, std::function<float(int)>, std::vector<float>&); _Args = {std::vector<int, std::allocator<int> >&, std::function<float(int)>&, std::vector<float, std::allocator<float> >&}]’
Error is much longer than that and I think its a bad idea to copy full error message which is about 3 pages long