Created
September 2, 2016 15:21
-
-
Save boxdot/5ed7600b343bcab7d283953d11efec84 to your computer and use it in GitHub Desktop.
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> | |
template<typename... Fn> | |
struct overload; | |
template<typename F, typename... Fn> | |
struct overload<F, Fn...> : F, public overload<Fn...>::type | |
{ | |
using type = overload; | |
overload(F f, Fn... fn) | |
: F(f) | |
, overload<Fn...>::type(fn...) | |
{} | |
using F::operator(); | |
using overload<Fn...>::type::operator(); | |
}; | |
template<typename F> | |
struct overload<F> : F { | |
using type = F; | |
using F::operator(); | |
}; | |
template<typename... Fn> | |
typename overload<Fn...>::type make_overload(Fn... fn) | |
{ | |
return overload<Fn...>(fn...); | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
auto fun = make_overload( | |
[](int& x) { x = 1; }, | |
[](float& x) { x = 2; }, | |
[](double& x) { x = 3; } | |
); | |
int x; | |
float y; | |
double z; | |
fun(x); | |
fun(y); | |
fun(z); | |
std::cout << x << std::endl; | |
std::cout << y << std::endl; | |
std::cout << z << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment