Created
October 27, 2022 13:35
-
-
Save lemire/74413c8c0f53a8222b4f5e86a6f7a66d to your computer and use it in GitHub Desktop.
Passing lambdas as values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <functional> | |
int g(int x, int y) { | |
auto lambda = [] <typename T>(std::function<T(T,T)> f, T x, T y) { return f(x,y); }; | |
std::function<int(int,int)> f = [](int x, int y)->int {return x + y;}; | |
return lambda(f, x,y); | |
} | |
int g2(int x, int y) { | |
auto lambda = [] <typename FNC, typename T>(FNC f, T x, T y) { return f(x,y); }; | |
return lambda([](int x, int y)->int {return x + y;}, x,y); | |
} | |
int g3(int x, int y) { | |
auto lambda = [] (auto f, auto x, auto y) { return f(x,y); }; | |
return lambda([](int x, int y)->int {return x + y;}, x,y); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment