Created
January 1, 2015 15:03
-
-
Save lucidguppy/f4e8f4315965594a6224 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <iostream> | |
#include <functional> | |
using namespace std; | |
using namespace std::placeholders; | |
int add(int a, int b) { | |
return a+b; | |
} | |
int subtract(int a, int b) { | |
return a-b; | |
} | |
int range_check(std::function<int(int,int)> func, int a, int b) { | |
cout << "Range check\n"; | |
if (a>5) { | |
cout << "A is greater than 5\n"; | |
} | |
if (b<6) { | |
cout << "B is less than 6\n"; | |
} | |
return func(a,b); | |
} | |
auto rangeCheckedAdd = bind(range_check, add, _1, _2); | |
auto rangeCheckedSubtract = bind(range_check, subtract, _1, _2); | |
int main() { | |
cout << "hello\n"; | |
int a = 10; | |
int b = 5; | |
cout << a << "+" << b << "=" << add(a,b) << "\n"; | |
cout << a << "-" << b << "=" << subtract(a,b) << "\n"; | |
cout << a << "+" << b << "=" << range_check(add,a,b) << "\n"; | |
cout << "New add:" << a << "+" << b << "=" << rangeCheckedAdd(a,b) << "\n"; | |
cout << "New subtract:" << a << "-" << b << "=" << rangeCheckedSubtract(a,b) << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment