re: loki技法(1).靜態斷言 主打歌 2010-09-06 23:10
讓boost, c++設計新思維統統去見鬼吧。看哥的靜態斷言的巧妙實現吧。
可以在下列范圍內使用(已經在vc,gcc中測試):
在名字空間域中使用 ;
在函數域中使用 ;
在類域中使用 ;
在類模板中使用 。
在函數模板中使用 。
比boost強大的是關鍵是還能支持c語言,不使用模版使得編譯時間大大縮短!!還能夠打印自定義的錯誤信息(我想這比啥都強)。
注意:gcc 的c編譯器居然允許0除以0的結果作為數組長度。此時只有警告,但是程序啟動就會立即產生浮點數異常,退出。g++工作正常。vc的c編譯器工作正常。
#define JOIN(x,y) x ## y
#define JOIN_1(x,y) JOIN(x, y)
#define JOIN_2(x,y) JOIN_1(x, y)
/*It's the begin of all error message.*/
#define ERRORMSG_BEGIN ATTENTION__The_assertion_error_is__
/*
*To fetch a error if testnum==0||testnum==false, by divide 0.
*Cast to int to avoid warning caused by bool division.
*/
#define IF_ERROR(testnum) (((int)(testnum))/((int)(testnum)))
/*
*The errormsg only support following characters: [a-zA-Z0-9] and '_'.
*Because it's a part of variable's name.
*Example:STATIC_ASSERT(sizeof(int) <= sizeof(short), The_sizeof_int_is_larger_than_short__)
*For above example, (Most) compilers can print error include following message:
ATTENTION__The_assertion_error_is__The_sizeof_int_is_larger_than_short__234
*The number in the end is the linenumber of the position of the macro been called.
*/
#ifndef NDEBUG
#ifndef _MSC_VER
#define STATIC_ASSERT(testnum, errormsg) static char JOIN_2(JOIN_1(ERRORMSG_BEGIN,errormsg),__LINE__)[IF_ERROR(testnum)];
#else
#define STATIC_ASSERT(testnum, errormsg) static char JOIN_1(ERRORMSG_BEGIN,errormsg)[IF_ERROR(testnum)];
#endif
#else
#define STATIC_ASSERT(testnum, errormsg)
#endif
//有看不懂英文的請留言咨詢。
正好2000年是閏年,不然你慘了。不過正好100年內不錯,不管了。
還有那個夏時令的標志最好清空設置
ret_time->tm_isdst = 0;
多謝先生了。