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. 4 5 #ifndef BASE_TEMPLATE_UTIL_H_ 6 #define BASE_TEMPLATE_UTIL_H_ 7 8 #include <stddef.h> 9 10 #include <iosfwd> 11 #include <iterator> 12 #include <type_traits> 13 #include <utility> 14 15 #include "base/compiler_specific.h" 16 17 namespace base { 18 19 namespace internal { 20 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 {}; 26 27 template <typename T> 28 struct is_iterator< 29 T, 30 std::void_t<typename std::iterator_traits<T>::iterator_category>> 31 : std::true_type {}; 32 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. 36 template <size_t I> 37 struct priority_tag : priority_tag<I - 1> {}; 38 39 template <> 40 struct priority_tag<0> {}; 41 42 } // namespace internal 43 44 namespace internal { 45 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 {}; 50 51 template <typename T> 52 struct IsScopedEnumImpl<T, /*std::is_enum_v<T>=*/true> 53 : std::negation<std::is_convertible<T, std::underlying_type_t<T>>> {}; 54 55 } // namespace internal 56 57 // Implementation of C++23's std::is_scoped_enum 58 // 59 // Reference: https://en.cppreference.com/w/cpp/types/is_scoped_enum 60 template <typename T> 61 struct is_scoped_enum : internal::IsScopedEnumImpl<T> {}; 62 63 // Implementation of C++20's std::remove_cvref. 64 // 65 // References: 66 // - https://en.cppreference.com/w/cpp/types/remove_cvref 67 // - https://wg21.link/meta.trans.other#lib:remove_cvref 68 template <typename T> 69 struct remove_cvref { 70 using type = std::remove_cv_t<std::remove_reference_t<T>>; 71 }; 72 73 // Implementation of C++20's std::remove_cvref_t. 74 // 75 // References: 76 // - https://en.cppreference.com/w/cpp/types/remove_cvref 77 // - https://wg21.link/meta.type.synop#lib:remove_cvref_t 78 template <typename T> 79 using remove_cvref_t = typename remove_cvref<T>::type; 80 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`. 85 // 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; 90 }; 91 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 95 // object in C++20. 96 }; 97 template <typename T> 98 struct IterValuePointerImpl<T*, true> { 99 using value_type = typename std::iterator_traits<T*>::value_type; 100 }; 101 102 template <typename T> 103 struct IterValueImpl<T*> { 104 using value_type = 105 typename IterValuePointerImpl<T*, std::is_object_v<T>>::value_type; 106 }; 107 108 template <typename Iter> 109 using iter_value_t = typename IterValueImpl<remove_cvref_t<Iter>>::value_type; 110 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`. 114 // 115 // Reference: https://wg21.link/iterator.synopsis#:~:text=iter_reference_t 116 template <typename Iter> 117 using iter_reference_t = decltype(*std::declval<Iter&>()); 118 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`. 122 // 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>...>; 127 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 132 // friendliness. 133 // 134 // Reference: https://wg21.link/projected 135 template <typename Iter, 136 typename Proj, 137 typename IndirectResultT = indirect_result_t<Proj, Iter>> 138 struct projected { 139 using value_type = remove_cvref_t<IndirectResultT>; 140 141 IndirectResultT operator*() const; // not defined 142 }; 143 144 } // namespace base 145 146 #endif // BASE_TEMPLATE_UTIL_H_ 147