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).
这篇关于在这个“元素数量"中进行虚拟加法的目的是什么?宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在这个“元素数量"中进行虚拟加法的目的是什么?宏?


基础教程推荐
- 这个宏可以转换成函数吗? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- C++结构和函数声明。为什么它不能编译? 2022-11-07