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>
15 #include "build/build_config.h"
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 #if (defined(__GNUC__) && __GNUC__ < 5) || \
26 (defined(__GLIBCXX__) && __GLIBCXX__ == CR_GLIBCXX_5_0_0)
27 #define CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX
30 // This hacks around using gcc with libc++ which has some incompatibilies.
31 // - is_trivially_* doesn't work: https://llvm.org/bugs/show_bug.cgi?id=27538
32 // TODO(danakj): Remove this when android builders are all using a newer version
33 // of gcc, or the android ndk is updated to a newer libc++ that works with older
35 #if !defined(__clang__) && defined(_LIBCPP_VERSION)
36 #define CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX
41 template <class T> struct is_non_const_reference : std::false_type {};
42 template <class T> struct is_non_const_reference<T&> : std::true_type {};
43 template <class T> struct is_non_const_reference<const T&> : std::false_type {};
47 // Implementation detail of base::void_t below.
48 template <typename...>
53 } // namespace internal
55 // base::void_t is an implementation of std::void_t from C++17.
57 // We use |base::internal::make_void| as a helper struct to avoid a C++14
59 // http://en.cppreference.com/w/cpp/types/void_t
60 // http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#1558
61 template <typename... Ts>
62 using void_t = typename ::base::internal::make_void<Ts...>::type;
66 // Uses expression SFINAE to detect whether using operator<< would work.
67 template <typename T, typename = void>
68 struct SupportsOstreamOperator : std::false_type {};
70 struct SupportsOstreamOperator<T,
71 decltype(void(std::declval<std::ostream&>()
72 << std::declval<T>()))>
75 // Used to detech whether the given type is an iterator. This is normally used
76 // with std::enable_if to provide disambiguation for functions that take
77 // templatzed iterators as input.
78 template <typename T, typename = void>
79 struct is_iterator : std::false_type {};
83 void_t<typename std::iterator_traits<T>::iterator_category>>
86 } // namespace internal
88 // is_trivially_copyable is especially hard to get right.
89 // - Older versions of libstdc++ will fail to have it like they do for other
90 // type traits. This has become a subset of the second point, but used to be
91 // handled independently.
92 // - An experimental release of gcc includes most of type_traits but misses
93 // is_trivially_copyable, so we still have to avoid using libstdc++ in this
94 // case, which is covered by CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX.
95 // - When compiling libc++ from before r239653, with a gcc compiler, the
96 // std::is_trivially_copyable can fail. So we need to work around that by not
97 // using the one in libc++ in this case. This is covered by the
98 // CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX define, and is discussed in
99 // https://llvm.org/bugs/show_bug.cgi?id=27538#c1 where they point out that
100 // in libc++'s commit r239653 this is fixed by libc++ checking for gcc 5.1.
101 // - In both of the above cases we are using the gcc compiler. When defining
102 // this ourselves on compiler intrinsics, the __is_trivially_copyable()
103 // intrinsic is not available on gcc before version 5.1 (see the discussion in
104 // https://llvm.org/bugs/show_bug.cgi?id=27538#c1 again), so we must check for
106 // - When __is_trivially_copyable() is not available because we are on gcc older
107 // than 5.1, we need to fall back to something, so we use __has_trivial_copy()
108 // instead based on what was done one-off in bit_cast() previously.
110 // TODO(crbug.com/554293): Remove this when all platforms have this in the std
111 // namespace and it works with gcc as needed.
112 #if defined(CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX) || \
113 defined(CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX)
114 template <typename T>
115 struct is_trivially_copyable {
116 // TODO(danakj): Remove this when android builders are all using a newer version
117 // of gcc, or the android ndk is updated to a newer libc++ that does this for
120 static constexpr bool value = __is_trivially_copyable(T);
122 static constexpr bool value =
123 __has_trivial_copy(T) && __has_trivial_destructor(T);
128 using is_trivially_copyable = std::is_trivially_copyable<T>;
131 #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ <= 7
132 // Workaround for g++7 and earlier family.
133 // Due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80654, without this
134 // Optional<std::vector<T>> where T is non-copyable causes a compile error.
135 // As we know it is not trivially copy constructible, explicitly declare so.
136 template <typename T>
137 struct is_trivially_copy_constructible
138 : std::is_trivially_copy_constructible<T> {};
140 template <typename... T>
141 struct is_trivially_copy_constructible<std::vector<T...>> : std::false_type {};
143 // Otherwise use std::is_trivially_copy_constructible as is.
144 template <typename T>
145 using is_trivially_copy_constructible = std::is_trivially_copy_constructible<T>;
148 // base::in_place_t is an implementation of std::in_place_t from
149 // C++17. A tag type used to request in-place construction in template vararg
153 // https://en.cppreference.com/w/cpp/utility/in_place
154 struct in_place_t {};
155 constexpr in_place_t in_place = {};
157 // base::in_place_type_t is an implementation of std::in_place_type_t from
158 // C++17. A tag type used for in-place construction when the type to construct
159 // needs to be specified, such as with base::unique_any, designed to be a
160 // drop-in replacement.
163 // http://en.cppreference.com/w/cpp/utility/in_place
164 template <typename T>
165 struct in_place_type_t {};
167 template <typename T>
168 struct is_in_place_type_t {
169 static constexpr bool value = false;
172 template <typename... Ts>
173 struct is_in_place_type_t<in_place_type_t<Ts...>> {
174 static constexpr bool value = true;
179 #undef CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX
180 #undef CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX
182 #endif // BASE_TEMPLATE_UTIL_H_