Skip to content

Commit

Permalink
Bug 766347 - Implement a mozilla::EnableIf template struct for SFINAE…
Browse files Browse the repository at this point in the history
… capabilities. r=luke
  • Loading branch information
jswalden committed Jun 19, 2012
1 parent ad263de commit b6433ce
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions mfbt/TypeTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,34 @@ 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_ */

0 comments on commit b6433ce

Please sign in to comment.