VC6对模板支持的很不好,_countof()是在VC6以后才增加的

来源:互联网 发布:怎么查131458淘宝信誉 编辑:程序博客网 时间:2024/05/29 06:42

VC6对模板支持的很不好,一些复杂的模板语法就编译不了。根据我的测试,就这里所说的_countof(),在VC6里就编译不了。VC2005/2008就已经支持的很好的。所以这个_countof()是在VC6以后才增加的。在VC6所带的stdlib.h里没有这个_countof()宏的。


摘自MS VC++ stdlib.h

#if !defined (__cplusplus)
//这种形式有点常识的人都能懂
#define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))  
#else /* !defined (__cplusplus) */
//但是下面这几行,我就搞不明白了
extern "C++"
{
template <typename _CountofType, size_t _SizeOfArray>
char (*__countof_helper(UNALIGNED _CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray];
#define _countof(_Array) sizeof(*__countof_helper(_Array))
}
#endif /* !defined (__cplusplus) */

原创粉丝点击