Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2504)

Unified Diff: base/template_util.h

Issue 2261793002: Bring the feedback button to the Mac sad tab (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Trybots caught this, but not the local presubmit check. Will investigate. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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_

Powered by Google App Engine
This is Rietveld 408576698