MADNESS 0.10.1
function_traits.h
Go to the documentation of this file.
1#ifndef MADNESS_FUNCTION_TRAITS
2#define MADNESS_FUNCTION_TRAITS
3
4#include <type_traits>
5
6namespace madness {
7 namespace detail {
8 // helps to implement other metafunctions
9 template<typename> struct is_type : public std::true_type { };
10
11 /// Function traits in the spirit of boost function traits
12 template <typename functionT, typename enablerT = void>
13 struct function_traits : public std::false_type {};
14
15 /// Function traits in the spirit of boost function traits
16 template <typename functionT, typename enablerT = void>
17 struct callable_traits : public std::false_type {};
18
19 /// Member function traits in the spirit of boost function traits
20 template <typename memfuncT>
21 struct memfunc_traits : public std::false_type { };
22
23 /// Function trait specialization for a free function pointer
24 template <typename resultT, typename... argTs>
25 struct function_traits<resultT(*)(argTs...), void> {
26 static const bool value = true;
27 static const int arity = sizeof...(argTs);
28 using result_type = resultT;
29 };
30
31 /// Function trait specialization for a *reference* to a free function pointer
32 template <typename resultT, typename... argTs>
33 struct function_traits<resultT(*&)(argTs...), void> {
34 static const bool value = true;
35 static const int arity = sizeof...(argTs);
36 using result_type = resultT;
37 };
38
39 /// Function trait specialization for a callable (can be a function, a (generic) lambda, etc.)
40 template <typename fnT, typename... argTs>
41 struct function_traits<fnT(argTs...), std::enable_if_t<is_type<std::invoke_result_t<fnT, argTs...>>::value>> {
42 static const bool value = true;
43 static const int arity = sizeof...(argTs);
44 using result_type = std::invoke_result_t<fnT, argTs...>;
45 };
46
47 /// Function traits in the spirit of boost function traits
48 template <typename fnT, typename... argTs>
49 struct callable_traits<fnT(argTs...), std::enable_if_t<is_type<std::invoke_result_t<fnT, argTs...>>::value>> {
50 static const bool value = true;
51 static const int arity = sizeof...(argTs);
52 using result_type = std::invoke_result_t<fnT, argTs...>;
53 };
54
55 /// Member function traits in the spirit of boost function traits
56 template <typename objT, typename returnT, typename... argTs>
57 struct memfunc_traits<returnT(objT::*)(argTs...)> {
58 static const bool value = true;
59 static const int arity = sizeof...(argTs);
60 static const bool constness = false;
61 typedef objT obj_type;
62 typedef returnT result_type;
63 };
64
65 /// Member function traits in the spirit of boost function traits
66 template <typename objT, typename returnT, typename... argTs>
67 struct memfunc_traits<returnT(objT::*)(argTs...) const> {
68 static const bool value = true;
69 static const int arity = sizeof...(argTs);
70 static const bool constness = true;
71 typedef objT obj_type;
72 typedef returnT result_type;
73 };
74
75
76 template <typename fnT, typename Enabler = void>
77 struct is_functor {
78 typedef char (& yes)[1];
79 typedef char (& no)[2];
80
81 // we need a template here to enable SFINAE
82 template <typename U>
83 static yes deduce(char (*)[sizeof(&U::operator())]);
84 // fallback
85 template <typename> static no deduce(...);
86
87 static const bool value = sizeof(deduce<fnT>(0)) == sizeof(yes);
88 };
89
90 template <typename fnT>
91 struct is_functor<fnT, typename std::enable_if<is_type<typename fnT::result_type>::value >::type> :
92 public std::true_type
93 { };
94
95 template <typename fnT, typename Enabler = void>
96 struct result_of {
97 typedef typename fnT::result_type type;
98 };
99
100 template <typename fnT>
101 struct result_of<fnT, typename std::enable_if<is_type<decltype(&fnT::operator())>::value >::type> :
102 public result_of<decltype(&fnT::operator())>
103 { };
104
105 template <typename fnT>
106 struct result_of<fnT, typename std::enable_if<function_traits<fnT>::value>::type> {
108 };
109
110 template <typename fnT>
111 struct result_of<fnT, typename std::enable_if<memfunc_traits<fnT>::value>::type> {
113 };
114 }
115}
116#endif
Namespace for all elements and tools of MADNESS.
Definition DFParameters.h:10
std::string type(const PairType &n)
Definition PNOParameters.h:18
Definition mraimpl.h:50
Function traits in the spirit of boost function traits.
Definition function_traits.h:17
resultT result_type
Definition function_traits.h:28
Function traits in the spirit of boost function traits.
Definition function_traits.h:13
Definition function_traits.h:77
char(& no)[2]
Definition function_traits.h:79
static yes deduce(char(*)[sizeof(&U::operator())])
char(& yes)[1]
Definition function_traits.h:78
static const bool value
Definition function_traits.h:87
Definition function_traits.h:9
returnT result_type
Definition function_traits.h:62
Member function traits in the spirit of boost function traits.
Definition function_traits.h:21
function_traits< fnT >::result_type type
Definition function_traits.h:107
memfunc_traits< fnT >::result_type type
Definition function_traits.h:112
Definition function_traits.h:96
fnT::result_type type
Definition function_traits.h:97