Created
April 27, 2017 17:42
-
-
Save marty1885/6649277ea332b200dbfc04e062f801c1 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 <functional> | |
#include <iostream> | |
using namespace std; | |
template<typename TFunc, typename FFunc>void If(bool condition, TFunc tFunc, | |
FFunc fFunc) | |
{ | |
std::function<void()> jumpTable[2] = {fFunc,tFunc}; | |
jumpTable[static_cast<int>(condition)](); | |
}; | |
template<typename TFunc>void If(bool condition, TFunc tFunc) | |
{ | |
If(condition, tFunc, [](){}); | |
}; | |
template<typename ConditionFunc, typename Func> | |
void While(ConditionFunc conditionFunc, Func func) | |
{ | |
auto compFunc = [&]() -> bool {return conditionFunc();}; | |
If(compFunc(), | |
[&](){ | |
func(); | |
While(conditionFunc, func); | |
}); | |
} | |
template<typename InitFunc, typename ConditionFunc | |
, typename ItFunc, typename Func> | |
void For(InitFunc initFunc, ConditionFunc conditionFunc | |
, ItFunc itFunc, Func func) | |
{ | |
initFunc(); | |
While(conditionFunc, [&]() | |
{ | |
func(); | |
itFunc(); | |
}); | |
} | |
int main() | |
{ | |
If([](){return 1 < 10;},[]() | |
{ | |
cout << "1 is smaller than 10" << endl; | |
}, []() | |
{ | |
cout << "1 is not smaller than 10" << endl; | |
}); | |
cout << endl; | |
int i = 0; | |
While([&](){return i<10;},[&]() | |
{ | |
cout << i << endl; | |
i++; | |
}); | |
cout << endl; | |
For([&](){i=0;},[&](){return i<10;},[&](){i++;},[&]() | |
{ | |
cout << i << endl; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment