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)
Run Code Online (Sandbox Code Playgroud)
它使用一个聪明的模板技巧来推断数组大小并防止指针传入__countof.
+ 0宏定义的目的是什么?它解决了什么问题?
我正在学习C++ 11,我偶然发现了统一的初始化器.
我不明白以下代码应该显示"最令人烦恼的解析"歧义:
#include<iostream>
class Timer
{
public:
Timer() {}
};
int main()
{
auto dv = Timer(); // What is Timer() ? And what type is dv?
int time_keeper(Timer()); // This is a function right? And why isn't the argument " Timer (*) ()" ?
return 0;
}
Run Code Online (Sandbox Code Playgroud)