Skip to content

Instantly share code, notes, and snippets.

@lemire
Created October 27, 2022 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lemire/74413c8c0f53a8222b4f5e86a6f7a66d to your computer and use it in GitHub Desktop.
Save lemire/74413c8c0f53a8222b4f5e86a6f7a66d to your computer and use it in GitHub Desktop.
Passing lambdas as values
#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