-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_traits.cpp
More file actions
37 lines (27 loc) · 941 Bytes
/
function_traits.cpp
File metadata and controls
37 lines (27 loc) · 941 Bytes
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
34
35
36
#include <iostream>
#include <tuple>
// decltype for all function (member or not ) will return a type as
// RT(...)(Args) so
// members will be T::* and non-members or statics will be (*) ...
// (*) => function
// * => others
template <class T> // here we will trigger second specialization with &T::operator() => (S::*)(Args...)
struct function_traits: public function_traits<decltype(&T::operator())>{};
template <class CT, class RT, class...Args>
struct function_traits<RT(CT::*)(Args...) const > {
enum {
num_args = sizeof...(Args)
};
typedef RT result_type;
template <size_t N>
struct arg {
typedef typename std::tuple_element<N, std::tuple<Args...>>::type type;
};
};
struct S {
void operator()(int,float,double) const {}
};
int main() {
std::cout << function_traits<S>::num_args << std::endl;
std::cout << typeid(function_traits<S>::arg<2>::type).name() << std::endl;
}