Index: base/template_util.h |
diff --git a/base/template_util.h b/base/template_util.h |
index 1bfc1ac814a23ceb186a9fcf6fa5924281d92783..4c9299b8a46f129490b030acab7e82d7d39af48e 100644 |
--- a/base/template_util.h |
+++ b/base/template_util.h |
@@ -126,6 +126,40 @@ template <class T> |
using is_trivially_destructible = std::is_trivially_destructible<T>; |
#endif |
+// 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) { |
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
|
+ 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 |
#undef CR_USE_FALLBACKS_FOR_OLD_GLIBCXX |