Created
September 25, 2012 01:45
-
-
Save dabrahams/3779508 to your computer and use it in GitHub Desktop.
A different rendition of https://gist.github.com/3779345
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
// Copyright Dave Abrahams 2012. Distributed under the Boost | |
// Software License, Version 1.0. (See accompanying | |
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
#include <utility> | |
template<class F1, class F2> | |
struct overload_set : F1, F2 | |
{ | |
overload_set(F1 x, F2 y) : F1(x), F2(y) {} | |
using F1::operator(); | |
using F2::operator(); | |
}; | |
#define RETURNS(...) \ | |
noexcept(noexcept(decltype(__VA_ARGS__)(std::move(__VA_ARGS__)))) \ | |
-> decltype(__VA_ARGS__) \ | |
{ return (__VA_ARGS__); } \ | |
static_assert(true, "") /* */ | |
template<class F> | |
F overload(F x) { return x; } | |
template<class F, class...Fs> | |
auto overload(F x, Fs...xs) | |
RETURNS( overload_set<F,decltype(overload(xs...))>(x, overload(xs...)) ); | |
auto f = overload( | |
[](int x) { return x+1; }, | |
[](char const* y) { return y + 1; }, | |
[](int* y) { return y; }); | |
int main() | |
{ | |
int a = f(1); | |
char const* p = f("hello"); | |
int* r = f(&a); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment