Created
September 25, 2012 00:50
-
-
Save dabrahams/3779345 to your computer and use it in GitHub Desktop.
Demonstration of Mathias Gaunard's overloaded function object technique. See also https://gist.github.com/3779508
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) | |
template<class...Fs> struct overloaded; | |
template<class F1, class...Fs> | |
struct overloaded<F1, Fs...> : F1, overloaded<Fs...>::type | |
{ | |
typedef overloaded type; | |
overloaded(F1 head, Fs...tail) | |
: F1(head), | |
overloaded<Fs...>::type(tail...) | |
{} | |
using F1::operator(); | |
using overloaded<Fs...>::type::operator(); | |
}; | |
template<class F> | |
struct overloaded<F> : F | |
{ | |
typedef F type; | |
using F::operator(); | |
}; | |
template<class...Fs> | |
typename overloaded<Fs...>::type overload(Fs...x) | |
{ return overloaded<Fs...>(x...); } | |
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); | |
} |
In generic code for unary case overload
type of returning value does not match the returning type of the function.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was very impressed by http://cpp-next.com/archive/2012/09/unifying-generic-functions-and-function-objects/
and was trying to find a variadic implementation:
I ended up with:
What gets me is that this works with just one
Can you explain to me why?
Thanks.