language: C++11 (gcc-4.7.2)
date: 478 days 6 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// http://stackoverflow.com/questions/9054703/overloaded-function-as-argument-of-variadic-template-function/9058164#9058164
 
#include<iostream>
using namespace std;
 
int sumall(int a) { return a; }
int sumall(int a, int b) { return a+b; }
string sumall(string a, string b) { return a+" "+b; }
 
template<typename ...Args>
struct OverloadResolved {
        template<typename R>
        static auto static_doit( R (*f) (Args...), Args ... args ) -> R {
                return f(args...);
        }
};
 
template<typename ...Args>
auto deduce(Args...) -> OverloadResolved<Args...> {
        return OverloadResolved<Args...>();
}
 
#define doit(f, ...) ( dummy<decltype(deduce( __VA_ARGS__ ))> :: static_doit(f, __VA_ARGS__) )
 
template<typename T>
struct dummy : public T { };
 
int main() {
   cout << doit(sumall, 7, 6) << endl;
   cout << doit(sumall, 10) << endl;
   cout << doit(sumall, string("hi"), string("world")) << endl;
}