1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_TEMPLATE_UTIL_H_
6 #define BASE_TEMPLATE_UTIL_H_
11 #include <type_traits>
14 #include "base/compiler_specific.h"
15 #include "build/build_config.h"
17 #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ <= 7
21 // Some versions of libstdc++ have partial support for type_traits, but misses
22 // a smaller subset while removing some of the older non-standard stuff. Assume
23 // that all versions below 5.0 fall in this category, along with one 5.0
24 // experimental release. Test for this by consulting compiler major version,
25 // the only reliable option available, so theoretically this could fail should
26 // you attempt to mix an earlier version of libstdc++ with >= GCC5. But
27 // that's unlikely to work out, especially as GCC5 changed ABI.
28 #define CR_GLIBCXX_5_0_0 20150123
29 #if (defined(__GNUC__) && __GNUC__ < 5) || \
30 (defined(__GLIBCXX__) && __GLIBCXX__ == CR_GLIBCXX_5_0_0)
31 #define CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX
34 // This hacks around using gcc with libc++ which has some incompatibilies.
35 // - is_trivially_* doesn't work: https://llvm.org/bugs/show_bug.cgi?id=27538
36 // TODO(danakj): Remove this when android builders are all using a newer version
37 // of gcc, or the android ndk is updated to a newer libc++ that works with older
39 #if !defined(__clang__) && defined(_LIBCPP_VERSION)
40 #define CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX
45 template <class T> struct is_non_const_reference : std::false_type {};
46 template <class T> struct is_non_const_reference<T&> : std::true_type {};
47 template <class T> struct is_non_const_reference<const T&> : std::false_type {};
51 // Implementation detail of base::void_t below.
52 template <typename...>
57 } // namespace internal
59 // base::void_t is an implementation of std::void_t from C++17.
61 // We use |base::internal::make_void| as a helper struct to avoid a C++14
63 // http://en.cppreference.com/w/cpp/types/void_t
64 // http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#1558
65 template <typename... Ts>
66 using void_t = typename ::base::internal::make_void<Ts...>::type;
70 // Uses expression SFINAE to detect whether using operator<< would work.
71 template <typename T, typename = void>
72 struct SupportsOstreamOperator : std::false_type {};
74 struct SupportsOstreamOperator<T,
75 decltype(void(std::declval<std::ostream&>()
76 << std::declval<T>()))>
79 template <typename T, typename = void>
80 struct SupportsToString : std::false_type {};
82 struct SupportsToString<T, decltype(void(std::declval<T>().ToString()))>
85 // Used to detech whether the given type is an iterator. This is normally used
86 // with std::enable_if to provide disambiguation for functions that take
87 // templatzed iterators as input.
88 template <typename T, typename = void>
89 struct is_iterator : std::false_type {};
93 void_t<typename std::iterator_traits<T>::iterator_category>>
96 // Helper to express preferences in an overload set. If more than one overload
97 // are available for a given set of parameters the overload with the higher
98 // priority will be chosen.
100 struct priority_tag : priority_tag<I - 1> {};
103 struct priority_tag<0> {};
105 } // namespace internal
107 // is_trivially_copyable is especially hard to get right.
108 // - Older versions of libstdc++ will fail to have it like they do for other
109 // type traits. This has become a subset of the second point, but used to be
110 // handled independently.
111 // - An experimental release of gcc includes most of type_traits but misses
112 // is_trivially_copyable, so we still have to avoid using libstdc++ in this
113 // case, which is covered by CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX.
114 // - When compiling libc++ from before r239653, with a gcc compiler, the
115 // std::is_trivially_copyable can fail. So we need to work around that by not
116 // using the one in libc++ in this case. This is covered by the
117 // CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX define, and is discussed in
118 // https://llvm.org/bugs/show_bug.cgi?id=27538#c1 where they point out that
119 // in libc++'s commit r239653 this is fixed by libc++ checking for gcc 5.1.
120 // - In both of the above cases we are using the gcc compiler. When defining
121 // this ourselves on compiler intrinsics, the __is_trivially_copyable()
122 // intrinsic is not available on gcc before version 5.1 (see the discussion in
123 // https://llvm.org/bugs/show_bug.cgi?id=27538#c1 again), so we must check for
125 // - When __is_trivially_copyable() is not available because we are on gcc older
126 // than 5.1, we need to fall back to something, so we use __has_trivial_copy()
127 // instead based on what was done one-off in bit_cast() previously.
129 // TODO(crbug.com/554293): Remove this when all platforms have this in the std
130 // namespace and it works with gcc as needed.
131 #if defined(CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX) || \
132 defined(CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX)
133 template <typename T>
134 struct is_trivially_copyable {
135 // TODO(danakj): Remove this when android builders are all using a newer version
136 // of gcc, or the android ndk is updated to a newer libc++ that does this for
139 static constexpr bool value = __is_trivially_copyable(T);
141 static constexpr bool value =
142 __has_trivial_copy(T) && __has_trivial_destructor(T);
147 using is_trivially_copyable = std::is_trivially_copyable<T>;
150 #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ <= 7
151 // Workaround for g++7 and earlier family.
152 // Due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80654, without this
153 // absl::optional<std::vector<T>> where T is non-copyable causes a compile
154 // error. As we know it is not trivially copy constructible, explicitly declare
156 template <typename T>
157 struct is_trivially_copy_constructible
158 : std::is_trivially_copy_constructible<T> {};
160 template <typename... T>
161 struct is_trivially_copy_constructible<std::vector<T...>> : std::false_type {};
163 // Otherwise use std::is_trivially_copy_constructible as is.
164 template <typename T>
165 using is_trivially_copy_constructible = std::is_trivially_copy_constructible<T>;
168 // base::in_place_t is an implementation of std::in_place_t from
169 // C++17. A tag type used to request in-place construction in template vararg
173 // https://en.cppreference.com/w/cpp/utility/in_place
174 struct in_place_t {};
175 constexpr in_place_t in_place = {};
177 // base::in_place_type_t is an implementation of std::in_place_type_t from
178 // C++17. A tag type used for in-place construction when the type to construct
179 // needs to be specified, such as with base::unique_any, designed to be a
180 // drop-in replacement.
183 // http://en.cppreference.com/w/cpp/utility/in_place
184 template <typename T>
185 struct in_place_type_t {};
187 template <typename T>
188 struct is_in_place_type_t {
189 static constexpr bool value = false;
192 template <typename... Ts>
193 struct is_in_place_type_t<in_place_type_t<Ts...>> {
194 static constexpr bool value = true;
197 // C++14 implementation of C++17's std::bool_constant.
199 // Reference: https://en.cppreference.com/w/cpp/types/integral_constant
200 // Specification: https://wg21.link/meta.type.synop
202 using bool_constant = std::integral_constant<bool, B>;
204 // C++14 implementation of C++17's std::conjunction.
206 // Reference: https://en.cppreference.com/w/cpp/types/conjunction
207 // Specification: https://wg21.link/meta.logical#1.itemdecl:1
208 template <typename...>
209 struct conjunction : std::true_type {};
211 template <typename B1>
212 struct conjunction<B1> : B1 {};
214 template <typename B1, typename... Bn>
215 struct conjunction<B1, Bn...>
216 : std::conditional_t<static_cast<bool>(B1::value), conjunction<Bn...>, B1> {
219 // C++14 implementation of C++17's std::disjunction.
221 // Reference: https://en.cppreference.com/w/cpp/types/disjunction
222 // Specification: https://wg21.link/meta.logical#itemdecl:2
223 template <typename...>
224 struct disjunction : std::false_type {};
226 template <typename B1>
227 struct disjunction<B1> : B1 {};
229 template <typename B1, typename... Bn>
230 struct disjunction<B1, Bn...>
231 : std::conditional_t<static_cast<bool>(B1::value), B1, disjunction<Bn...>> {
234 // C++14 implementation of C++17's std::negation.
236 // Reference: https://en.cppreference.com/w/cpp/types/negation
237 // Specification: https://wg21.link/meta.logical#itemdecl:3
238 template <typename B>
239 struct negation : bool_constant<!static_cast<bool>(B::value)> {};
241 // Implementation of C++17's invoke_result.
243 // This implementation adds references to `Functor` and `Args` to work around
244 // some quirks of std::result_of. See the #Notes section of [1] for details.
247 // [1] https://en.cppreference.com/w/cpp/types/result_of
248 // [2] https://wg21.link/meta.trans.other#lib:invoke_result
249 template <typename Functor, typename... Args>
250 using invoke_result = std::result_of<Functor && (Args && ...)>;
252 // Implementation of C++17's std::invoke_result_t.
254 // Reference: https://wg21.link/meta.type.synop#lib:invoke_result_t
255 template <typename Functor, typename... Args>
256 using invoke_result_t = typename invoke_result<Functor, Args...>::type;
260 // Base case, `InvokeResult` does not have a nested type member. This means `F`
261 // could not be invoked with `Args...` and thus is not invocable.
262 template <typename InvokeResult, typename R, typename = void>
263 struct IsInvocableImpl : std::false_type {};
265 // Happy case, `InvokeResult` does have a nested type member. Now check whether
266 // `InvokeResult::type` is convertible to `R`. Short circuit in case
267 // `std::is_void<R>`.
268 template <typename InvokeResult, typename R>
269 struct IsInvocableImpl<InvokeResult, R, void_t<typename InvokeResult::type>>
270 : disjunction<std::is_void<R>,
271 std::is_convertible<typename InvokeResult::type, R>> {};
273 } // namespace internal
275 // Implementation of C++17's std::is_invocable_r.
277 // Returns whether `F` can be invoked with `Args...` and the result is
278 // convertible to `R`.
280 // Reference: https://wg21.link/meta.rel#lib:is_invocable_r
281 template <typename R, typename F, typename... Args>
282 struct is_invocable_r
283 : internal::IsInvocableImpl<invoke_result<F, Args...>, R> {};
285 // Implementation of C++17's std::is_invocable.
287 // Returns whether `F` can be invoked with `Args...`.
289 // Reference: https://wg21.link/meta.rel#lib:is_invocable
290 template <typename F, typename... Args>
291 struct is_invocable : is_invocable_r<void, F, Args...> {};
295 // The indirection with std::is_enum<T> is required, because instantiating
296 // std::underlying_type_t<T> when T is not an enum is UB prior to C++20.
297 template <typename T, bool = std::is_enum<T>::value>
298 struct IsScopedEnumImpl : std::false_type {};
300 template <typename T>
301 struct IsScopedEnumImpl<T, /*std::is_enum<T>::value=*/true>
302 : negation<std::is_convertible<T, std::underlying_type_t<T>>> {};
304 } // namespace internal
306 // Implementation of C++23's std::is_scoped_enum
308 // Reference: https://en.cppreference.com/w/cpp/types/is_scoped_enum
309 template <typename T>
310 struct is_scoped_enum : internal::IsScopedEnumImpl<T> {};
312 // Implementation of C++20's std::remove_cvref.
315 // - https://en.cppreference.com/w/cpp/types/remove_cvref
316 // - https://wg21.link/meta.trans.other#lib:remove_cvref
317 template <typename T>
318 struct remove_cvref {
319 using type = std::remove_cv_t<std::remove_reference_t<T>>;
322 // Implementation of C++20's std::remove_cvref_t.
325 // - https://en.cppreference.com/w/cpp/types/remove_cvref
326 // - https://wg21.link/meta.type.synop#lib:remove_cvref_t
327 template <typename T>
328 using remove_cvref_t = typename remove_cvref<T>::type;
330 // Implementation of C++20's std::is_constant_evaluated.
333 // - https://en.cppreference.com/w/cpp/types/is_constant_evaluated
334 // - https://wg21.link/meta.const.eval
335 constexpr bool is_constant_evaluated() noexcept {
336 #if HAS_BUILTIN(__builtin_is_constant_evaluated)
337 return __builtin_is_constant_evaluated();
343 // Simplified implementation of C++20's std::iter_value_t.
344 // As opposed to std::iter_value_t, this implementation does not restrict
345 // the type of `Iter` and does not consider specializations of
346 // `indirectly_readable_traits`.
348 // Reference: https://wg21.link/readable.traits#2
349 template <typename Iter>
351 typename std::iterator_traits<remove_cvref_t<Iter>>::value_type;
353 // Simplified implementation of C++20's std::iter_reference_t.
354 // As opposed to std::iter_reference_t, this implementation does not restrict
355 // the type of `Iter`.
357 // Reference: https://wg21.link/iterator.synopsis#:~:text=iter_reference_t
358 template <typename Iter>
359 using iter_reference_t = decltype(*std::declval<Iter&>());
361 // Simplified implementation of C++20's std::indirect_result_t. As opposed to
362 // std::indirect_result_t, this implementation does not restrict the type of
363 // `Func` and `Iters`.
365 // Reference: https://wg21.link/iterator.synopsis#:~:text=indirect_result_t
366 template <typename Func, typename... Iters>
367 using indirect_result_t = invoke_result_t<Func, iter_reference_t<Iters>...>;
369 // Simplified implementation of C++20's std::projected. As opposed to
370 // std::projected, this implementation does not explicitly restrict the type of
371 // `Iter` and `Proj`, but rather does so implicitly by requiring
372 // `indirect_result_t<Proj, Iter>` is a valid type. This is required for SFINAE
375 // Reference: https://wg21.link/projected
376 template <typename Iter,
378 typename IndirectResultT = indirect_result_t<Proj, Iter>>
380 using value_type = remove_cvref_t<IndirectResultT>;
382 IndirectResultT operator*() const; // not defined
387 #undef CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX
388 #undef CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX
390 #endif // BASE_TEMPLATE_UTIL_H_