Skip to content

Instantly share code, notes, and snippets.

@boxdot
Created September 2, 2016 15:21
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 boxdot/5ed7600b343bcab7d283953d11efec84 to your computer and use it in GitHub Desktop.
Save boxdot/5ed7600b343bcab7d283953d11efec84 to your computer and use it in GitHub Desktop.
#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