1 // Copyright 2011 The Chromium Authors
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_
12 #include <type_traits>
15 #include "base/compiler_specific.h"
21 // Used to detech whether the given type is an iterator. This is normally used
22 // with std::enable_if to provide disambiguation for functions that take
23 // templatzed iterators as input.
24 template <typename T, typename = void>
25 struct is_iterator : std::false_type {};
30 std::void_t<typename std::iterator_traits<T>::iterator_category>>
33 // Helper to express preferences in an overload set. If more than one overload
34 // are available for a given set of parameters the overload with the higher
35 // priority will be chosen.
37 struct priority_tag : priority_tag<I - 1> {};
40 struct priority_tag<0> {};
42 } // namespace internal
46 // The indirection with std::is_enum<T> is required, because instantiating
47 // std::underlying_type_t<T> when T is not an enum is UB prior to C++20.
48 template <typename T, bool = std::is_enum_v<T>>
49 struct IsScopedEnumImpl : std::false_type {};
52 struct IsScopedEnumImpl<T, /*std::is_enum_v<T>=*/true>
53 : std::negation<std::is_convertible<T, std::underlying_type_t<T>>> {};
55 } // namespace internal
57 // Implementation of C++23's std::is_scoped_enum
59 // Reference: https://en.cppreference.com/w/cpp/types/is_scoped_enum
61 struct is_scoped_enum : internal::IsScopedEnumImpl<T> {};
63 // Implementation of C++20's std::remove_cvref.
66 // - https://en.cppreference.com/w/cpp/types/remove_cvref
67 // - https://wg21.link/meta.trans.other#lib:remove_cvref
70 using type = std::remove_cv_t<std::remove_reference_t<T>>;
73 // Implementation of C++20's std::remove_cvref_t.
76 // - https://en.cppreference.com/w/cpp/types/remove_cvref
77 // - https://wg21.link/meta.type.synop#lib:remove_cvref_t
79 using remove_cvref_t = typename remove_cvref<T>::type;
81 // Simplified implementation of C++20's std::iter_value_t.
82 // As opposed to std::iter_value_t, this implementation does not restrict
83 // the type of `Iter` and does not consider specializations of
84 // `indirectly_readable_traits`.
86 // Reference: https://wg21.link/readable.traits#2
87 template <typename Iter>
88 struct IterValueImpl {
89 using value_type = typename std::iterator_traits<Iter>::value_type;
92 template <typename T, bool Cond = false>
93 struct IterValuePointerImpl {
94 // The `iterator_traits<T*>::value_type` member is not defined if T is not an
98 struct IterValuePointerImpl<T*, true> {
99 using value_type = typename std::iterator_traits<T*>::value_type;
102 template <typename T>
103 struct IterValueImpl<T*> {
105 typename IterValuePointerImpl<T*, std::is_object_v<T>>::value_type;
108 template <typename Iter>
109 using iter_value_t = typename IterValueImpl<remove_cvref_t<Iter>>::value_type;
111 // Simplified implementation of C++20's std::iter_reference_t.
112 // As opposed to std::iter_reference_t, this implementation does not restrict
113 // the type of `Iter`.
115 // Reference: https://wg21.link/iterator.synopsis#:~:text=iter_reference_t
116 template <typename Iter>
117 using iter_reference_t = decltype(*std::declval<Iter&>());
119 // Simplified implementation of C++20's std::indirect_result_t. As opposed to
120 // std::indirect_result_t, this implementation does not restrict the type of
121 // `Func` and `Iters`.
123 // Reference: https://wg21.link/iterator.synopsis#:~:text=indirect_result_t
124 template <typename Func, typename... Iters>
125 using indirect_result_t =
126 std::invoke_result_t<Func, iter_reference_t<Iters>...>;
128 // Simplified implementation of C++20's std::projected. As opposed to
129 // std::projected, this implementation does not explicitly restrict the type of
130 // `Iter` and `Proj`, but rather does so implicitly by requiring
131 // `indirect_result_t<Proj, Iter>` is a valid type. This is required for SFINAE
134 // Reference: https://wg21.link/projected
135 template <typename Iter,
137 typename IndirectResultT = indirect_result_t<Proj, Iter>>
139 using value_type = remove_cvref_t<IndirectResultT>;
141 IndirectResultT operator*() const; // not defined
146 #endif // BASE_TEMPLATE_UTIL_H_