Skip to content

Instantly share code, notes, and snippets.

@JustinSDK
Created January 8, 2020 06:02
Show Gist options
  • Save JustinSDK/23f135ced38809b719ba772ad54fa423 to your computer and use it in GitHub Desktop.
Save JustinSDK/23f135ced38809b719ba772ad54fa423 to your computer and use it in GitHub Desktop.
return recursive lambda
#include <iostream>
#include <functional>
using namespace std;
function<int(int)> foo() {
return [](int n) -> int {
function<int(int)> fac = [&](int n) -> int {
return n == 0 ? 1 : n * fac(n-1);
};
return fac(n);
};
}
int main(int, char*[]) {
cout << foo()(4) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment