所謂的compile-time Constraints就是在編譯期使用C++的語法規則對設計進行約束檢查,盡量在編譯期把程序的問題暴露出來,節省成本。
常用的規則:
1.大小為0的數組在編譯期會報錯
比如
check_size<int,long>();//ok
check_size<int,char>();//compile error:cannot allocate an array of constant size 0
2.不同類型的指針需要強制轉換
以此可以用來判斷兩個對象是否有繼承關系
is_derived<int,char>() //compile failed
3.union的成員不能是非POD
union Test
{
std::string s; //illegal
};
這一點可以用來測試一個類型是否是POD
check_pod<int>();//ok
check_pod<std::string>();//failed
常用的技巧:
1.利用模板特化
Loki里面的例子
2.利用空函數來實現0開銷
編譯器可能將空函數優化掉
3.利用enum無開銷的取得編譯期的所得到的整數值
Loki里面的例子:
常用的規則:
1.大小為0的數組在編譯期會報錯
template<typename T1,typename T2>
void check_size()
{
int dmp[sizeof(T1)==sizeof(T2)];
}
void check_size()
{
int dmp[sizeof(T1)==sizeof(T2)];
}
比如
check_size<int,long>();//ok
check_size<int,char>();//compile error:cannot allocate an array of constant size 0
2.不同類型的指針需要強制轉換
以此可以用來判斷兩個對象是否有繼承關系
template<typename T1,typename T2>
void is_derived(T1 *pd =0 ,T2 *pb=0)
{
pb = pd;
}
比如:void is_derived(T1 *pd =0 ,T2 *pb=0)
{
pb = pd;
}
is_derived<int,char>() //compile failed
3.union的成員不能是非POD
union Test
{
std::string s; //illegal
};
這一點可以用來測試一個類型是否是POD
template<typename T>
void check_pod()
{
union {
T data;
};
};
比如:void check_pod()
{
union {
T data;
};
};
check_pod<int>();//ok
check_pod<std::string>();//failed
常用的技巧:
1.利用模板特化
Loki里面的例子
namespace Loki
{
template<int> struct CompileTimeError;
template<> struct CompileTimeError<true> {};
}
#define LOKI_STATIC_CHECK(expr, msg) \
{ Loki::CompileTimeError<((expr) != 0)> ERROR_##msg; (void)ERROR_##msg; }
利用特化來定制錯誤信息{
template<int> struct CompileTimeError;
template<> struct CompileTimeError<true> {};
}
#define LOKI_STATIC_CHECK(expr, msg) \
{ Loki::CompileTimeError<((expr) != 0)> ERROR_##msg; (void)ERROR_##msg; }
2.利用空函數來實現0開銷
編譯器可能將空函數優化掉
3.利用enum無開銷的取得編譯期的所得到的整數值
Loki里面的例子:
template <class TList> struct Length;
template <> struct Length<NullType>
{
enum { value = 0 };
};
template <class T, class U>
struct Length< Typelist<T, U> >
{
enum { value = 1 + Length<U>::value };
};
template <> struct Length<NullType>
{
enum { value = 0 };
};
template <class T, class U>
struct Length< Typelist<T, U> >
{
enum { value = 1 + Length<U>::value };
};