Bug 1289033 - Add mozilla::UnderlyingType. r?froydnj draft
authorCameron McCormack <cam@mcc.id.au>
Mon, 25 Jul 2016 16:06:12 +0800
changeset 392250 d5b9373111499b7588c73aad48487a85f6463a6f
parent 392248 93d4bd8a4ad33d4202c8705e1a4e4fe716843614
child 526288 99a2ab907f7464743e07b5501164ed4c5b6796d7
push id23977
push userbmo:cam@mcc.id.au
push dateMon, 25 Jul 2016 08:06:50 +0000
reviewersfroydnj
bugs1289033
milestone50.0a1
Bug 1289033 - Add mozilla::UnderlyingType. r?froydnj MozReview-Commit-ID: GOCMsqHbMmJ
mfbt/TypeTraits.h
--- a/mfbt/TypeTraits.h
+++ b/mfbt/TypeTraits.h
@@ -1186,16 +1186,37 @@ struct Conditional
 };
 
 template<class A, class B>
 struct Conditional<false, A, B>
 {
   typedef B Type;
 };
 
+/**
+ * Converts an enum to its underlying type.
+ *
+ * enum A { ... };
+ * enum class B { ... };
+ * enum C : uint8_t { ... };
+ * enum class D : int { ... };
+ *
+ * mozilla::UnderlyingType<A>::Type is an unspecified integral type;
+ * mozilla::UnderlyingType<B>::Type is int;
+ * mozilla::UnderlyingType<C>::Type is uint8_t;
+ * mozilla::UnderlyingType<D>::Type is int.
+ */
+template<typename T>
+struct UnderlyingType
+{
+  // __underlying_type is a supported extension across all of our supported
+  // compilers.
+  typedef __underlying_type(T) Type;
+};
+
 namespace detail {
 
 template<typename U,
          bool IsArray = IsArray<U>::value,
          bool IsFunction = IsFunction<U>::value>
 struct DecaySelector;
 
 template<typename U>