Bug 953296 - Add IsLvalueReference and IsVoid. r=jcranmer

--HG--
extra : rebase_source : 53def7d45901da43ffbcfef3adbbea3a3e6bb329
This commit is contained in:
Jeff Walden 2014-01-09 10:09:14 -06:00
Родитель b706ddaf73
Коммит 770045e23f
2 изменённых файлов: 76 добавлений и 3 удалений

Просмотреть файл

@ -49,6 +49,27 @@ typedef IntegralConstant<bool, false> FalseType;
namespace detail {
template<typename T>
struct IsVoidHelper : FalseType {};
template<>
struct IsVoidHelper<void> : TrueType {};
} // namespace detail
/**
* IsVoid determines whether a type is void.
*
* mozilla::IsVoid<int>::value is false;
* mozilla::IsVoid<void>::value is true;
* mozilla::IsVoid<void*>::value is false;
* mozilla::IsVoid<volatile void>::value is true.
*/
template<typename T>
struct IsVoid : detail::IsVoidHelper<typename RemoveCV<T>::Type> {};
namespace detail {
template <typename T>
struct IsIntegralHelper : FalseType {};
@ -688,14 +709,54 @@ struct RemoveReference<T&&>
typedef T Type;
};
template<bool Condition, typename A, typename B>
struct Conditional;
namespace detail {
enum Voidness { TIsVoid, TIsNotVoid };
template<typename T, Voidness V = IsVoid<T>::value ? TIsVoid : TIsNotVoid>
struct AddLvalueReferenceHelper;
template<typename T>
struct AddLvalueReferenceHelper<T, TIsVoid>
{
typedef void Type;
};
template<typename T>
struct AddLvalueReferenceHelper<T, TIsNotVoid>
{
typedef T& Type;
};
} // namespace detail
/**
* AddLvalueReference adds an lvalue & reference to T if one isn't already
* present. (Note: adding an lvalue reference to an rvalue && reference in
* essence replaces the && with a &&, per C+11 reference collapsing rules. For
* example, int&& would become int&.)
*
* The final computed type will only *not* be an lvalue reference if T is void.
*
* mozilla::AddLvalueReference<int>::Type is int&;
* mozilla::AddLvalueRference<volatile int&>::Type is volatile int&;
* mozilla::AddLvalueReference<void*>::Type is void*&;
* mozilla::AddLvalueReference<void>::Type is void;
* mozilla::AddLvalueReference<struct S&&>::Type is struct S&.
*/
template<typename T>
struct AddLvalueReference
: detail::AddLvalueReferenceHelper<T>
{};
/* 20.9.7.3 Sign modifications [meta.trans.sign] */
template<bool B, typename T = void>
struct EnableIf;
template<bool Condition, typename A, typename B>
struct Conditional;
namespace detail {
template<bool MakeConst, typename T>

Просмотреть файл

@ -6,6 +6,7 @@
#include "mozilla/Assertions.h"
#include "mozilla/TypeTraits.h"
using mozilla::AddLvalueReference;
using mozilla::IsArray;
using mozilla::IsBaseOf;
using mozilla::IsClass;
@ -242,6 +243,17 @@ TestIsConvertible()
// "C doesn't convert to A (private inheritance)");
}
static_assert(IsSame<AddLvalueReference<int>::Type, int&>::value,
"not adding & to int correctly");
static_assert(IsSame<AddLvalueReference<volatile int&>::Type, volatile int&>::value,
"not adding & to volatile int& correctly");
static_assert(IsSame<AddLvalueReference<void*>::Type, void*&>::value,
"not adding & to void* correctly");
static_assert(IsSame<AddLvalueReference<void>::Type, void>::value,
"void shouldn't be transformed by AddLvalueReference");
static_assert(IsSame<AddLvalueReference<struct S1&&>::Type, struct S1&>::value,
"not reference-collapsing struct S1&& & to struct S1& correctly");
static_assert(IsSame<MakeSigned<const unsigned char>::Type, const signed char>::value,
"const unsigned char won't signify correctly");
static_assert(IsSame<MakeSigned<volatile unsigned short>::Type, volatile signed short>::value,