Skip to content

Instantly share code, notes, and snippets.

@mazhar-ansari-ardeh
Created November 27, 2016 10:44
Show Gist options
  • Save mazhar-ansari-ardeh/d10e50a850bde3262fae23c8e5d8c242 to your computer and use it in GitHub Desktop.
Save mazhar-ansari-ardeh/d10e50a850bde3262fae23c8e5d8c242 to your computer and use it in GitHub Desktop.
A small program that demonstrates the definition and usages of lambda expressions in C++.
#include <functional>
#include <iostream>
#include <stdio.h>
using namespace std;
extern function<int(int, function<int(int)>)> g;
auto f = [](int n, function<int(int)> h)
{
if (n >= 5) return n;
cout << "h is some function " << h(4.0) << endl;
auto func = [h](int m) { return m*h(m); };
return g(n + 1, func);
};
function<int(int, function<int(int)>)> g = [](int n, function<int(int)> h)
{
if (n >= 5) return n;
cout << "h is some function " << h(5.0) << endl;
auto func = [h](int m) { return m*h(m); };
return f(n + 1, func);
};
int main()
{
auto initial_function = [](int m) { return m*m; };
f(1, initial_function);
getchar();
return 0; // Return value of the function is int so its better to use 0 rather than 0.0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment