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 detect 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 } // namespace base 64 65 #endif // BASE_TEMPLATE_UTIL_H_ 66