Created
November 28, 2012 21:24
-
-
Save socantre/4164652 to your computer and use it in GitHub Desktop.
c++ overload set type
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
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