vendredi 29 novembre 2019

Calling a templated funtion using a thread object

#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

Aucun commentaire:

Enregistrer un commentaire