Index: base/template_util.h |
diff --git a/base/template_util.h b/base/template_util.h |
index 1bfc1ac814a23ceb186a9fcf6fa5924281d92783..d09e98b60d3446f7aa7e7560e8058d263d0486ec 100644 |
--- a/base/template_util.h |
+++ b/base/template_util.h |
@@ -126,8 +126,42 @@ template <class T> |
using is_trivially_destructible = std::is_trivially_destructible<T>; |
#endif |
-} // namespace base |
- |
#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!
|
+// underlying_value |
+// Casts an enum to its underlying integral type, leaves other values alone. |
+// Useful for scoped enums, which don't cast implicitly. |
+ |
+// Enums, taken by value. This specialization is necessary because creating an |
+// std::underlying_type with a non-enum is UB (and usually a compile error). |
+template <typename T, |
+ typename std::enable_if<std::is_enum<T>::value, int>::type = 0> |
+constexpr typename underlying_type<T>::type underlying_value(T val) { |
+ return static_cast<typename underlying_type<T>::type>(val); |
+} |
+ |
+// Non-enum scalars, taken by value. This specialization is necessary due to an |
+// interesting case where a static const integral or enum data member can have |
+// an inline initializer and no out-of-line definition, but then may only be |
+// used in constant expressions. The catch-all specialization below would try |
+// to form a const reference. Discussion here: |
+// http://stackoverflow.com/questions/3792412/const-and-static-specifiers-in-c/3792427#3792427 |
+template <typename T, |
+ typename std::enable_if<std::is_fundamental<T>::value, int>::type = 0> |
+constexpr T underlying_value(T val) { |
+ return val; |
+} |
+ |
+// Non-scalars, taken by rvalue reference. T must be copyable or moveable, just |
+// like a static_cast. |
+template <typename T, |
+ typename std::enable_if< |
+ !std::is_scalar<typename std::remove_reference<T>::type>::value, |
+ int>::type = 0> |
+constexpr T underlying_value(T&& val) { |
+ return std::forward<T>(val); |
+} |
+ |
+} // namespace base |
+ |
#endif // BASE_TEMPLATE_UTIL_H_ |