Visual C++ 10 附带 stdlib.h,其中包含此 gem:
template <typename _CountofType, size_t _SizeOfArray>
char (*__countof_helper(UNALIGNED _CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray];
#define _countof(_Array) (sizeof(*__countof_helper(_Array)) + 0)
它使用 a clever template trick to deduce array size并防止将指针传递到 __countof
。
宏定义中+ 0
的作用是什么?它解决了什么问题?
最佳答案
从 here 引用 STL
I made this change; I don't usually hack the CRT, but this one was trivial. The + 0 silences a spurious "warning C6260: sizeof * sizeof is usually wrong. Did you intend to use a character count or a byte count?" from /analyze when someone writes _countof(arr) * sizeof(T).
关于c++ - 在此 "number of elements"宏中添加虚拟字符的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9679097/