Skip to content

Instantly share code, notes, and snippets.

@dabrahams
Created September 25, 2012 01:45
Show Gist options
  • Save dabrahams/3779508 to your computer and use it in GitHub Desktop.
Save dabrahams/3779508 to your computer and use it in GitHub Desktop.
A different rendition of https://gist.github.com/3779345
// 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