Skip to content

Instantly share code, notes, and snippets.

@B1Z0N
Created July 1, 2019 23:42
Show Gist options
  • Save B1Z0N/3e9d7edc3987b1ba0c6d2bde51cb47b2 to your computer and use it in GitHub Desktop.
Save B1Z0N/3e9d7edc3987b1ba0c6d2bde51cb47b2 to your computer and use it in GitHub Desktop.
#include <functional>
#include <iostream>
using namespace std;
template <typename... RetTs, typename... ArgTs>
auto _for_each(std::function<RetTs(ArgTs...)>... funcs)
{
return [ = ] (ArgTs... args) {
return tuple {
funcs(args...)...
};
};
}
template <typename... FuncTs>
auto for_each(FuncTs... funcs)
{
return _for_each(std::function{funcs} ...);
}
auto f1(char a)
{
cout << a; return '1';
}
auto f2(char a)
{
cout << a; return 2 ;
}
int main()
{
auto l1 {[](char a) { cout << a; return '1'; }};
auto l2 {[](char a) { cout << a; return 2 ; }};
// testing with lambdas
auto rets {for_each(l1, l2)('a')};
cout << endl << get<0>(rets) << endl;
cout << get<1>(rets) << endl;
std::function func1 {l1};
std::function func2 {l2};
// testing with std::function`s
rets = for_each(func1, func2)('a');
cout << endl << get<0>(rets) << endl;
cout << get<1>(rets) << endl;
// testing with ordinary functions
rets = for_each(f1, f2)('a');
cout << endl << get<0>(rets) << endl;
cout << get<1>(rets) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment