vendredi 6 avril 2018

C++ Calling lambda in function

I am trying to call lambda inside the function but something is wrong. Can you explain what exactly is going wrong?

#include<iostream>
#include<algorithm>
#include<vector>
#include <functional>

int f1(int &x)
//std::function<int(int)> f1(int &x)
{
    return [x]() {return x * 2;};

}

int main()
{
    int y = { 5 };
    int x=f1(y);
    //int x=f1(y)();
    std::cout<<x<<"\n";
    return 0;
}

Lambda will do x * 2 and will return int.

Errors I am getting are

- no suitable conversion function from "lambda []int ()->int" to "int" exists   
- 'return': cannot convert from 'f1::<lambda_011db7c58818224ec010a58ca6d>' to 'std::function<int (int)>'    

Aucun commentaire:

Enregistrer un commentaire