Skip to content

Instantly share code, notes, and snippets.

@socantre
Created November 28, 2012 21:24
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 socantre/4164652 to your computer and use it in GitHub Desktop.
Save socantre/4164652 to your computer and use it in GitHub Desktop.
c++ overload set type
template <class... Fs> struct overload_set;
template <class F>
struct overload_set<F> : F
{
overload_set(F &&x) : F(std::forward<F>(x)) {}
using F::operator();
};
template <class F, class... Fs>
struct overload_set<F, Fs...> : F, overload_set<Fs...>
{
overload_set(F &&x, Fs &&... xs) : F(std::forward<F>(x)), overload_set<Fs...>(std::forward<Fs>(xs)...) {}
using F::operator();
using overload_set<Fs...>::operator();
};
template <class... Fs>
overload_set<Fs...> overload(Fs &&... xs)
{
return overload_set<Fs...>(std::forward<Fs>(xs)...);
}
int main() {
auto o = overload(
[](int){ std::printf("Hello, "); },
[](double){ std::printf("World!\n"); }
);
o(1);
o(1.);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment