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 // underlying_value | |
| 130 // Casts an enum to its underlying integral type, leaves other values alone. | |
| 131 // Useful for scoped enums, which don't cast implicitly. | |
| 132 | |
| 133 // Enums, taken by value. This specialization is necessary because creating an | |
| 134 // std::underlying_type with a non-enum is UB (and usually a compile error). | |
| 135 template <typename T, | |
| 136 typename std::enable_if<std::is_enum<T>::value, int>::type = 0> | |
| 137 constexpr typename underlying_type<T>::type underlying_value(T val) { | |
|
Nico
2016/09/08 17:12:59
This should be called UnderlyingType. This file is
Sidney San Martín
2016/09/09 01:36:00
Done. You're right that it should be left to a sep
| |
| 138 return static_cast<typename underlying_type<T>::type>(val); | |
| 139 } | |
| 140 | |
| 141 // Non-enum scalars, taken by value. This specialization is necessary due to an | |
| 142 // interesting case where a static const integral or enum data member can have | |
| 143 // an inline initializer and no out-of-line definition, but then may only be | |
| 144 // used in constant expressions. The catch-all specialization below would try | |
| 145 // to form a const reference. Discussion here: | |
| 146 // http://stackoverflow.com/questions/3792412/const-and-static-specifiers-in-c/3 792427#3792427 | |
| 147 template <typename T, | |
| 148 typename std::enable_if<std::is_fundamental<T>::value, int>::type = 0> | |
| 149 constexpr T underlying_value(T val) { | |
| 150 return val; | |
| 151 } | |
| 152 | |
| 153 // Non-scalars, taken by rvalue reference. T must be copyable or moveable, just | |
| 154 // like a static_cast. | |
| 155 template <typename T, | |
| 156 typename std::enable_if< | |
| 157 !std::is_scalar<typename std::remove_reference<T>::type>::value, | |
| 158 int>::type = 0> | |
| 159 constexpr T underlying_value(T&& val) { | |
| 160 return std::forward<T>(val); | |
| 161 } | |
| 162 | |
| 129 } // namespace base | 163 } // namespace base |
| 130 | 164 |
| 131 #undef CR_USE_FALLBACKS_FOR_OLD_GLIBCXX | 165 #undef CR_USE_FALLBACKS_FOR_OLD_GLIBCXX |
| 132 | 166 |
| 133 #endif // BASE_TEMPLATE_UTIL_H_ | 167 #endif // BASE_TEMPLATE_UTIL_H_ |
| OLD | NEW |