Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_TEMPLATE_UTIL_H_ | 5 #ifndef BASE_TEMPLATE_UTIL_H_ |
| 6 #define BASE_TEMPLATE_UTIL_H_ | 6 #define BASE_TEMPLATE_UTIL_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <iosfwd> | 9 #include <iosfwd> |
| 10 #include <type_traits> | 10 #include <type_traits> |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 // TODO(crbug.com/554293): Remove this when all platforms have this in the std | 119 // TODO(crbug.com/554293): Remove this when all platforms have this in the std |
| 120 // namespace. | 120 // namespace. |
| 121 #if defined(CR_USE_FALLBACKS_FOR_OLD_GLIBCXX) | 121 #if defined(CR_USE_FALLBACKS_FOR_OLD_GLIBCXX) |
| 122 template <class T> | 122 template <class T> |
| 123 using is_trivially_destructible = std::has_trivial_destructor<T>; | 123 using is_trivially_destructible = std::has_trivial_destructor<T>; |
| 124 #else | 124 #else |
| 125 template <class T> | 125 template <class T> |
| 126 using is_trivially_destructible = std::is_trivially_destructible<T>; | 126 using is_trivially_destructible = std::is_trivially_destructible<T>; |
| 127 #endif | 127 #endif |
| 128 | 128 |
| 129 #undef CR_USE_FALLBACKS_FOR_OLD_GLIBCXX | |
|
Ilya Sherman
2016/08/30 21:06:39
nit: Why did you move this? If you don't need to
Sidney San Martín
2016/08/30 21:18:13
I did have to move it, sadly. It needs the underly
Sidney San Martín
2016/08/30 21:30:32
Sorry, just figured out what you meant. Fixed!
| |
| 130 | |
| 131 // underlying_value | |
| 132 // Casts an enum to its underlying integral type, leaves other values alone. | |
| 133 // Useful for scoped enums, which don't cast implicitly. | |
| 134 | |
| 135 // Enums, taken by value. This specialization is necessary because creating an | |
| 136 // std::underlying_type with a non-enum is UB (and usually a compile error). | |
| 137 template <typename T, | |
| 138 typename std::enable_if<std::is_enum<T>::value, int>::type = 0> | |
| 139 constexpr typename underlying_type<T>::type underlying_value(T val) { | |
| 140 return static_cast<typename underlying_type<T>::type>(val); | |
| 141 } | |
| 142 | |
| 143 // Non-enum scalars, taken by value. This specialization is necessary due to an | |
| 144 // interesting case where a static const integral or enum data member can have | |
| 145 // an inline initializer and no out-of-line definition, but then may only be | |
| 146 // used in constant expressions. The catch-all specialization below would try | |
| 147 // to form a const reference. Discussion here: | |
| 148 // http://stackoverflow.com/questions/3792412/const-and-static-specifiers-in-c/3 792427#3792427 | |
| 149 template <typename T, | |
| 150 typename std::enable_if<std::is_fundamental<T>::value, int>::type = 0> | |
| 151 constexpr T underlying_value(T val) { | |
| 152 return val; | |
| 153 } | |
| 154 | |
| 155 // Non-scalars, taken by rvalue reference. T must be copyable or moveable, just | |
| 156 // like a static_cast. | |
| 157 template <typename T, | |
| 158 typename std::enable_if< | |
| 159 !std::is_scalar<typename std::remove_reference<T>::type>::value, | |
| 160 int>::type = 0> | |
| 161 constexpr T underlying_value(T&& val) { | |
| 162 return std::forward<T>(val); | |
| 163 } | |
| 164 | |
| 129 } // namespace base | 165 } // namespace base |
| 130 | 166 |
| 131 #undef CR_USE_FALLBACKS_FOR_OLD_GLIBCXX | |
| 132 | |
| 133 #endif // BASE_TEMPLATE_UTIL_H_ | 167 #endif // BASE_TEMPLATE_UTIL_H_ |
| OLD | NEW |