Last active
January 25, 2016 03:26
-
-
Save Masstronaut/abe58fda221696cbed3b to your computer and use it in GitHub Desktop.
Variadic "overloading" of lambdas.
This file contains 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 <string> | |
#include <ostream> | |
template<typename... Predicates> | |
struct overload; | |
template<typename Pred, typename... Predicates> | |
struct overload<Pred, Predicates...> : Pred, overload<Predicates...> | |
{ | |
overload(Pred p, Predicates... preds) | |
: Pred(p) | |
, overload<Predicates...>(preds...) | |
{ | |
} | |
using Pred::operator(); | |
using overload<Predicates...>::operator(); | |
}; | |
template<typename Predicate> | |
struct overload<Predicate> : Predicate | |
{ | |
overload(Predicate p) | |
: Predicate(p) | |
{ | |
} | |
using Predicate::operator(); | |
}; | |
template<typename... Predicates> | |
auto make_overload(Predicates... preds) | |
{ | |
return overload<Predicates...>(preds...); | |
} | |
int main() { | |
auto print = make_overload | |
( | |
[](int i) { std::cout << i << std::endl; }, | |
[](std::string s) { std::cout << s << std::endl; }, | |
[](char c) { std::cout << c << std::endl; } | |
); | |
print(1); | |
print("Hello"); | |
print(15); | |
print('R'); | |
print("This is a string."); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment