type_traits.hpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2014, 2015 Ableton AG, Berlin. All rights reserved.
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21 //
22 
27 #pragma once
28 
29 #include <type_traits>
30 
31 namespace atria {
32 namespace estd {
33 
34 namespace detail {
35 
36 template<typename... Ts>
37 struct make_void
38 {
39  using type = void;
40 };
41 
42 } // namespace detail
43 
47 template<typename... Ts>
48 using void_t = typename detail::make_void<Ts...>::type;
49 
53 template <typename T>
54 using decay_t = typename std::decay<T>::type;
55 
59 template <typename T>
60 using result_of_t = typename std::result_of<T>::type;
61 
65 template <bool X, typename T, typename F>
66 using conditional_t = typename std::conditional<X, T, F>::type;
67 
71 template <typename T>
72 using remove_reference_t = typename std::remove_reference<T>::type;
73 
77 template <typename T>
78 using remove_const_t = typename std::remove_const<T>::type;
79 
83 template <bool X, typename T=void>
84 using enable_if_t = typename std::enable_if<X, T>::type;
85 
86 namespace detail {
87 
88 template <class Default, class AlwaysVoid,
89  template<class...> class Op, class... Args>
90 struct detector
91 {
92  using value_t = std::false_type;
93  using type = Default;
94 };
95 
96 template <class Default, template<class...> class Op, class... Args>
97 struct detector<Default, void_t<Op<Args...> >, Op, Args...>
98 {
99  using value_t = std::true_type;
100  using type = Op<Args...>;
101 };
102 
103 } // namespace detail
104 
108 struct nonesuch
109 {
110  nonesuch() = delete;
111  ~nonesuch() = delete;
112  nonesuch(nonesuch const&) = delete;
113  void operator=(nonesuch const&) = delete;
114 };
115 
119 template <template<class...> class Op, class... Args>
120 using is_detected =
121  typename detail::detector<nonesuch, void, Op, Args...>::value_t;
122 
126 template <template<class...> class Op, class... Args>
127 using detected_t =
128  typename detail::detector<nonesuch, void, Op, Args...>::type;
129 
133 template <class Default, template<class...> class Op, class... Args>
134 using detected_or = detail::detector<Default, void, Op, Args...>;
135 
139 template <class Default, template<class...> class Op, class... Args>
140 using detected_or_t = typename detected_or<Default, Op, Args...>::type;
141 
145 template <class Expected, template<class...> class Op, class... Args>
146 using is_detected_exact = std::is_same<Expected, detected_t<Op, Args...> >;
147 
151 template <class To, template<class...> class Op, class... Args>
153  std::is_convertible<detected_t<Op, Args...>, To>;
154 
155 #if ABL_CXX14
156 
160 template <template<class...> class Op, class... Args>
161 constexpr bool is_detected_v = is_detected<Op, Args...>::value;
162 
166 template <class Expected, template<class...> class Op, class... Args>
167 constexpr bool is_detected_exact_v =
168  is_detected_exact<Expected, Op, Args...>::value;
169 
173 template <class To, template<class...> class Op, class... Args>
174 constexpr bool is_detected_convertible_v =
175  is_detected_convertible<To, Op, Args...>::value;
176 
177 #endif // ABL_CXX14
178 
179 } // namespace estd
180 } // namespace atria
typename detail::detector< nonesuch, void, Op, Args... >::type detected_t
Similar to TS Fundamentals 2 std::detected_t
typename detail::make_void< Ts... >::type void_t
Similar to C++17 std::void_t.
Definition: type_traits.hpp:48
eval_t< get_value_type< estd::decay_t< T > > > value_t
Convenient alias for get_value_type
Definition: value_type.hpp:76
std::is_same< Expected, detected_t< Op, Args... > > is_detected_exact
Similar to TS Fundamentals 2 std::is_detected_exact
typename std::enable_if< X, T >::type enable_if_t
Similar to C++14 std::enable_if_t.
Definition: type_traits.hpp:84
typename std::decay< T >::type decay_t
Similar to C++14 std::decay_t.
Definition: type_traits.hpp:54
typename std::conditional< X, T, F >::type conditional_t
Similar to C++14 std::conditional_t.
Definition: type_traits.hpp:66
detail::detector< Default, void, Op, Args... > detected_or
Similar to TS Fundamentals 2 std::dected_or
typename std::remove_const< T >::type remove_const_t
Similar to C++14 std::remove_const_t.
Definition: type_traits.hpp:78
Similar to TS Fundamentals 2 std::nonesuch
std::is_convertible< detected_t< Op, Args... >, To > is_detected_convertible
Similar to TS Fundamentals 2 std::is_detected_exact_v
typename detail::detector< nonesuch, void, Op, Args... >::value_t is_detected
Similar to TS Fundamentals 2 std::is_detected
typename std::remove_reference< T >::type remove_reference_t
Similar to C++14 std::remove_reference_t.
Definition: type_traits.hpp:72
typename detected_or< Default, Op, Args... >::type detected_or_t
Similar to TS Fundamentals 2 std::dected_or_t
C++ amazing templates and reusable implementations awesomeness.
Definition: _doc.hpp:35
typename std::result_of< T >::type result_of_t
Similar to C++14 std::result_of_t.
Definition: type_traits.hpp:60
Fork me on GitHub