什么是虚设此外在该&QUOT的目的;元素&QUOT数;宏? [英] What's the purpose of dummy addition in this "number of elements" macro?

查看:203
本文介绍了什么是虚设此外在该&QUOT的目的;元素&QUOT数;宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的Visual C ++ 10附带的stdlib.h中除其他外包含此宝石:

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)

它使用一个聪明的模板伎俩演绎数组大小被传递到prevent指针 __ countof

什么是 0 + 的宏定义的目的是什么?它解决什么问题呢?

What's the purpose of + 0 in the macro definition? What problem does it solve?

推荐答案

从<一个引用STL href=\"http://channel9.msdn.com/Forums/TechOff/567789-template-countof-confusion-question\">here

我做了这个改变;我通常不破解CRT,但是这一次是
  不重要的。该+ 0沉默虚假警告C6260:sizeof的* sizeof的
  通常是错的。您是否打算使用字符数或字节
  算不算?从/分析时,有人写_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).

这篇关于什么是虚设此外在该&QUOT的目的;元素&QUOT数;宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆