Skip to content

Instantly share code, notes, and snippets.

@maddouri
Last active March 11, 2018 00:32
Show Gist options
  • Save maddouri/4f9431e51dc038ce72b16b5112beb55b to your computer and use it in GitHub Desktop.
Save maddouri/4f9431e51dc038ce72b16b5112beb55b to your computer and use it in GitHub Desktop.
#include <iostream>
// http://coliru.stacked-crooked.com/a/3d07f7711012d021
// https://stackoverflow.com/q/29661253/865719
// https://connect.microsoft.com/VisualStudio/feedback/details/990223/overloaded-lambda-pattern-fails-to-compile
template <typename L, typename... Ls>
struct overload_set : L, overload_set<Ls...>
{
using L::operator();
using overload_set<Ls...>::operator();
explicit overload_set(L l, Ls... ls) : L(l), overload_set<Ls...>(ls...) {}
};
template <typename L>
struct overload_set<L> : L
{
using L::operator();
explicit overload_set(L l) : L(l) {}
};
template <typename... Ls>
overload_set<Ls...> make_overload(Ls... ls)
{
return overload_set<Ls...>(ls...);
}
int main()
{
using std::cout;
using std::endl;
auto os = make_overload(
[](int) { cout << __PRETTY_FUNCTION__ << endl; },
[](double) { cout << __PRETTY_FUNCTION__ << endl; },
[](auto) { cout << __PRETTY_FUNCTION__ << endl; }
);
os(1);
os(1.9);
os('x');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment