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. 4 5 #ifndef BASE_TEMPLATE_UTIL_H_ 6 #define BASE_TEMPLATE_UTIL_H_ 7 8 #include <stddef.h> 9 #include <iosfwd> 10 #include <iterator> 11 #include <type_traits> 12 #include <utility> 13 #include <vector> 14 15 #include "build/build_config.h" 16 17 // Some versions of libstdc++ have partial support for type_traits, but misses 18 // a smaller subset while removing some of the older non-standard stuff. Assume 19 // that all versions below 5.0 fall in this category, along with one 5.0 20 // experimental release. Test for this by consulting compiler major version, 21 // the only reliable option available, so theoretically this could fail should 22 // you attempt to mix an earlier version of libstdc++ with >= GCC5. But 23 // that's unlikely to work out, especially as GCC5 changed ABI. 24 #define CR_GLIBCXX_5_0_0 20150123 25 // `!defined(__clang__)` is a local android patch 26 #if !defined(__clang__) && ((defined(__GNUC__) && __GNUC__ < 5) || \ 27 (defined(__GLIBCXX__) && __GLIBCXX__ == CR_GLIBCXX_5_0_0)) 28 #define CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX 29 #endif 30 31 // This hacks around using gcc with libc++ which has some incompatibilies. 32 // - is_trivially_* doesn't work: https://llvm.org/bugs/show_bug.cgi?id=27538 33 // TODO(danakj): Remove this when android builders are all using a newer version 34 // of gcc, or the android ndk is updated to a newer libc++ that works with older 35 // gcc versions. 36 #if !defined(__clang__) && defined(_LIBCPP_VERSION) 37 #define CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX 38 #endif 39 40 namespace base { 41 42 template <class T> struct is_non_const_reference : std::false_type {}; 43 template <class T> struct is_non_const_reference<T&> : std::true_type {}; 44 template <class T> struct is_non_const_reference<const T&> : std::false_type {}; 45 46 namespace internal { 47 48 // Implementation detail of base::void_t below. 49 template <typename...> 50 struct make_void { 51 using type = void; 52 }; 53 54 } // namespace internal 55 56 // base::void_t is an implementation of std::void_t from C++17. 57 // 58 // We use |base::internal::make_void| as a helper struct to avoid a C++14 59 // defect: 60 // http://en.cppreference.com/w/cpp/types/void_t 61 // http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#1558 62 template <typename... Ts> 63 using void_t = typename ::base::internal::make_void<Ts...>::type; 64 65 namespace internal { 66 67 // Uses expression SFINAE to detect whether using operator<< would work. 68 template <typename T, typename = void> 69 struct SupportsOstreamOperator : std::false_type {}; 70 template <typename T> 71 struct SupportsOstreamOperator<T, 72 decltype(void(std::declval<std::ostream&>() 73 << std::declval<T>()))> 74 : std::true_type {}; 75 76 // Used to detech whether the given type is an iterator. This is normally used 77 // with std::enable_if to provide disambiguation for functions that take 78 // templatzed iterators as input. 79 template <typename T, typename = void> 80 struct is_iterator : std::false_type {}; 81 82 template <typename T> 83 struct is_iterator<T, 84 void_t<typename std::iterator_traits<T>::iterator_category>> 85 : std::true_type {}; 86 87 } // namespace internal 88 89 // is_trivially_copyable is especially hard to get right. 90 // - Older versions of libstdc++ will fail to have it like they do for other 91 // type traits. This has become a subset of the second point, but used to be 92 // handled independently. 93 // - An experimental release of gcc includes most of type_traits but misses 94 // is_trivially_copyable, so we still have to avoid using libstdc++ in this 95 // case, which is covered by CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX. 96 // - When compiling libc++ from before r239653, with a gcc compiler, the 97 // std::is_trivially_copyable can fail. So we need to work around that by not 98 // using the one in libc++ in this case. This is covered by the 99 // CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX define, and is discussed in 100 // https://llvm.org/bugs/show_bug.cgi?id=27538#c1 where they point out that 101 // in libc++'s commit r239653 this is fixed by libc++ checking for gcc 5.1. 102 // - In both of the above cases we are using the gcc compiler. When defining 103 // this ourselves on compiler intrinsics, the __is_trivially_copyable() 104 // intrinsic is not available on gcc before version 5.1 (see the discussion in 105 // https://llvm.org/bugs/show_bug.cgi?id=27538#c1 again), so we must check for 106 // that version. 107 // - When __is_trivially_copyable() is not available because we are on gcc older 108 // than 5.1, we need to fall back to something, so we use __has_trivial_copy() 109 // instead based on what was done one-off in bit_cast() previously. 110 111 // TODO(crbug.com/554293): Remove this when all platforms have this in the std 112 // namespace and it works with gcc as needed. 113 #if defined(CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX) || \ 114 defined(CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX) 115 template <typename T> 116 struct is_trivially_copyable { 117 // TODO(danakj): Remove this when android builders are all using a newer version 118 // of gcc, or the android ndk is updated to a newer libc++ that does this for 119 // us. 120 #if _GNUC_VER >= 501 121 static constexpr bool value = __is_trivially_copyable(T); 122 #else 123 static constexpr bool value = 124 __has_trivial_copy(T) && __has_trivial_destructor(T); 125 #endif 126 }; 127 #else 128 template <class T> 129 using is_trivially_copyable = std::is_trivially_copyable<T>; 130 #endif 131 132 #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ <= 7 133 // Workaround for g++7 and earlier family. 134 // Due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80654, without this 135 // Optional<std::vector<T>> where T is non-copyable causes a compile error. 136 // As we know it is not trivially copy constructible, explicitly declare so. 137 template <typename T> 138 struct is_trivially_copy_constructible 139 : std::is_trivially_copy_constructible<T> {}; 140 141 template <typename... T> 142 struct is_trivially_copy_constructible<std::vector<T...>> : std::false_type {}; 143 #else 144 // Otherwise use std::is_trivially_copy_constructible as is. 145 template <typename T> 146 using is_trivially_copy_constructible = std::is_trivially_copy_constructible<T>; 147 #endif 148 149 } // namespace base 150 151 #undef CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX 152 #undef CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX 153 154 #endif // BASE_TEMPLATE_UTIL_H_ 155