What#39;s the purpose of dummy addition in this quot;number of elementsquot; macro?(在这个“元素数量中进行虚拟加法的目的是什么?宏?)
问题描述
Visual C++ 10 附带 stdlib.h,其中包含此 gem:
Visual C++ 10 is shipped with stdlib.h that among other things contains this gem:
template <typename _CountofType, size_t _SizeOfArray>
char (*__countof_helper(UNALIGNED _CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray];
#define _countof(_Array) (sizeof(*__countof_helper(_Array)) + 0)
它使用一个巧妙的模板技巧来推断数组大小和防止指针被传递到__countof.
宏定义中+0的作用是什么?它解决了什么问题?
What's the purpose of + 0 in the macro definition? What problem does it solve?
推荐答案
引用 这里
我做了这个改变;我通常不破解 CRT,但这个是不重要的.+ 0 使虚假的警告 C6260: sizeof * sizeof通常是错误的.您打算使用字符数还是字节数count?" 来自/analyze,当有人写 _countof(arr) * sizeof(T).
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++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- CString 到 char* 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
