From b6433ce02c8d8a1d4055e06a929e2ab0ce12f882 Mon Sep 17 00:00:00 2001 From: Jeff Walden Date: Mon, 18 Jun 2012 19:06:33 -0700 Subject: [PATCH] Bug 766347 - Implement a mozilla::EnableIf template struct for SFINAE capabilities. r=luke --- mfbt/TypeTraits.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/mfbt/TypeTraits.h b/mfbt/TypeTraits.h index 2d47b2cbf7bae..178ecdf26fc3d 100644 --- a/mfbt/TypeTraits.h +++ b/mfbt/TypeTraits.h @@ -49,6 +49,34 @@ struct Conditional typedef B Type; }; +/* + * EnableIf is a struct containing a typedef of T if and only if B is true. + * + * mozilla::EnableIf::Type is int; + * mozilla::EnableIf::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 + * class PodVector // vector optimized to store POD (memcpy-able) types + * { + * EnableIf, T>::Type* vector; + * size_t length; + * ... + * }; + */ +template +struct EnableIf +{}; + +template +struct EnableIf +{ + typedef T Type; +}; + } /* namespace mozilla */ #endif /* mozilla_TypeTraits_h_ */