Bug 766347 - Implement a mozilla::EnableIf template struct for SFINAE capabilities. r=luke
authorJeff Walden <jwalden@mit.edu>
Mon, 18 Jun 2012 19:06:33 -0700
changeset 102310 54a17b76f48868f24dfa261309289af896eca39d
parent 102309 8aa75c0cd1e5c22a84e0e514e985d7d4be2cab65
child 102311 fe817bf85f36386d626ed35383ffb9dae2f4ddc5
push id1316
push userakeybl@mozilla.com
push dateMon, 27 Aug 2012 22:37:00 +0000
treeherdermozilla-beta@db4b09302ee2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersluke
bugs766347
milestone16.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 766347 - Implement a mozilla::EnableIf template struct for SFINAE capabilities. r=luke
content/base/src/nsGenericElement.cpp
mfbt/TypeTraits.h
--- a/mfbt/TypeTraits.h
+++ b/mfbt/TypeTraits.h
@@ -44,11 +44,39 @@ struct Conditional
 };
 
 template<class A, class B>
 struct Conditional<false, A, B>
 {
     typedef B Type;
 };
 
+/*
+ * EnableIf is a struct containing a typedef of T if and only if B is true.
+ *
+ * mozilla::EnableIf<true, int>::Type is int;
+ * mozilla::EnableIf<false, int>::Type is a compile-time error.
+ *
+ * Use this template to implement SFINAE-style (Substitution Failure Is not An
+ * Error) requirements.  For example, you might use it to impose a restriction
+ * on a template parameter:
+ *
+ *   template<typename T>
+ *   class PodVector // vector optimized to store POD (memcpy-able) types
+ *   {
+ *      EnableIf<IsPodType<T>, T>::Type* vector;
+ *      size_t length;
+ *      ...
+ *   };
+ */
+template<bool B, typename T = void>
+struct EnableIf
+{};
+
+template<typename T>
+struct EnableIf<true, T>
+{
+    typedef T Type;
+};
+
 } /* namespace mozilla */
 
 #endif  /* mozilla_TypeTraits_h_ */