All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FunctionTraits.h
Go to the documentation of this file.
1 #ifndef FUNCTIONTRAITS_H
2 #define FUNCTIONTRAITS_H
3 
4 #include <type_traits>
5 #include <tuple>
6 
7 namespace Aux {
8 
9 // Code taken from https://functionalcpp.wordpress.com/2013/08/05/function-traits/ and slightly modified
10 
11 template<class F>
13 
14 // function pointer
15 template<class R, class... Args>
16 struct FunctionTraits<R( *)(Args...)> : public FunctionTraits<R(Args...)>
17 {};
18 
19 template<class R, class... Args>
20 struct FunctionTraits<R(Args...)> {
21  using result_type = R;
22 
23  static constexpr std::size_t arity = sizeof...(Args);
24 
25  template <std::size_t N>
26  struct arg;
27 
28  template <std::size_t N>
29  struct arg {
30  static_assert(N < arity, "error: invalid parameter index.");
31  using type = typename std::tuple_element<N, std::tuple<Args...>>::type;
32  };
33 };
34 
35 
36 // member function pointer
37 template<class C, class R, class... Args>
38 struct FunctionTraits<R(C:: *)(Args...)> : public FunctionTraits<R(C &, Args...)>
39 {};
40 
41 // const member function pointer
42 template<class C, class R, class... Args>
43 struct FunctionTraits<R(C:: *)(Args...) const> : public FunctionTraits<R(C &, Args...)>
44 {};
45 
46 // member object pointer
47 template<class C, class R>
48 struct FunctionTraits<R(C:: *)> : public FunctionTraits<R(C &)>
49 {};
50 
51 // functor
52 template<class F>
53 struct FunctionTraits {
54 private:
56 public:
58 
59  static constexpr std::size_t arity = call_type::arity - 1;
60 
61  template <std::size_t N>
62  struct arg;
63 
64  template <std::size_t N>
65  struct arg {
66  static_assert(N < arity, "error: invalid parameter index.");
67  using type = typename call_type::template arg < N + 1 >::type;
68  };
69 };
70 
71 template<class F>
72 struct FunctionTraits<F &> : public FunctionTraits<F>
73 {};
74 
75 template<class F>
76 struct FunctionTraits < F && > : public FunctionTraits<F>
77 {};
78 
79 
80 
81 } // namespace Aux
82 
83 #endif // FUNCTIONTRAITS_H
static constexpr std::size_t arity
Definition: FunctionTraits.h:59
typename call_type::result_type result_type
Definition: FunctionTraits.h:57
Definition: FunctionTraits.h:62
typename std::tuple_element< N, std::tuple< Args...>>::type type
Definition: FunctionTraits.h:31
R result_type
Definition: FunctionTraits.h:21
Definition: FunctionTraits.h:12
typename call_type::template arg< N+1 >::type type
Definition: FunctionTraits.h:67