c++ - 在此 "number of elements"宏中添加虚拟字符的目的是什么?

标签 c++ arrays templates visual-c++ macros

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/

相关文章:

PHP - 创建自定义模板系统?

C++模板成员初始化: move-construction with rvalue but reference with lvalue

c++ - binary_function,调用时出现编译器错误

c++ - 如何在 C++ 中使用 *& 传递数组

c++ - wrong-looking编译错误调用模板类的模板成员函数

c - 如何正确地将十六进制字符串转换为 C 中的字节数组?

javascript - 使用Underscore获取满足特定条件的元素的所有索引

c++ - 创建类型特征以检测 C++ 中的仿函数

c++ - std::forward Visual Studio 13 的行为与我预期的不同

c++ - 如何在 C++ 中声明 3 维字符串数组