Skip to content

Instantly share code, notes, and snippets.

@marty1885
Created April 27, 2017 17:42
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 marty1885/6649277ea332b200dbfc04e062f801c1 to your computer and use it in GitHub Desktop.
Save marty1885/6649277ea332b200dbfc04e062f801c1 to your computer and use it in GitHub Desktop.
#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