// 约束:必须是POD
namespace CoreTools
{
template <typename T>
struct MustBePod
{
~MustBePod()
{
void(*ptr)() = Constranints;
UNREFERENCED_PARAMETER(ptr);
}
private:
static void Constranints()
{
union
{
T TIsNotPODType;
} unionType;
UNREFERENCED_PARAMETER(unionType);
}
};
template <typename T>
struct MustBePodOrVoid
{
~MustBePodOrVoid()
{
void(*ptr)() = Constranints;
UNREFERENCED_PARAMETER(ptr);
}
private:
static void Constranints()
{
union
{
T TIsNotPODTypeOrVoid;
} unionType;
UNREFERENCED_PARAMETER(unionType);
}
};
template <>
struct MustBePodOrVoid<void>
{
};
}
// 约束:两个类大小必须相等,可能依赖于硬件架构或编译器。
namespace CoreTools
{
template<typename T>
struct SizeOf
{
enum
{
Value = sizeof(T)
};
};
template<>
struct SizeOf<void>
{
enum
{
Value = 0
};
};
template <typename T1,typename T2>
struct MustBeSameSize
{
~MustBeSameSize()
{
void(*ptr)() = Constranints;
UNREFERENCED_PARAMETER(ptr);
}
private:
static void Constranints()
{
const int T1NotSameSizeAsT2 =
(static_cast<int>(SizeOf<T1>::Value) ==
static_cast<int>(SizeOf<T2>::Value));
int i[T1NotSameSizeAsT2];
UNREFERENCED_PARAMETER(i[0]);
}
};
}
// 约束:必须可以按下标访问
namespace CoreTools
{
template <typename T>
struct MustBeSubscriptable
{
~MustBeSubscriptable()
{
void(*ptr)(const T&) = Constranints;
UNREFERENCED_PARAMETER(ptr);
}
private:
static void Constranints(const T& TIsNoSubscriptable)
{
int size = sizeof(TIsNoSubscriptable[0]);
UNREFERENCED_PARAMETER(size);
TIsNoSubscriptable[0];
}
};
template <typename T>
struct MustBeSubscriptableAsDecayablePointer
{
~MustBeSubscriptableAsDecayablePointer()
{
void(*ptr)(const T&) = Constranints;
UNREFERENCED_PARAMETER(ptr);
}
private:
static void Constranints(T const& TIsNoSubscriptableAsDecayablePointer)
{
int size = sizeof(0[TIsNoSubscriptableAsDecayablePointer]);
UNREFERENCED_PARAMETER(size);
UNREFERENCED_PARAMETER(0[TIsNoSubscriptableAsDecayablePointer]);
}
};
}
// 约束:类D是类B的派生类或相同类型
namespace CoreTools
{
template <typename D,typename B>
struct MustHaveBase
{
~MustHaveBase()
{
void(*ptr)(D*,B*) = Constranints;
UNREFERENCED_PARAMETER(ptr);
}
private:
static void Constranints(D* derivePtr,B* basePtr)
{
#if defined(__GNUC__)
basePtr = derivePtr;
if(basePtr != derivePtr)
{
}
#else
basePtr = derivePtr;
UNREFERENCED_PARAMETER(basePtr);
#endif // __GNUC__
}
};
}