约束

// 约束:必须是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__ 
}
};
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值