const // It is a const object...
class nullptr_t
{
public:
template<class T>
inline operator T*() const // convertible to any type of null non-member pointer...
{ return 0; }
template<class C, class T>
inline operator T C::*() const // or any type of null member pointer...
{ return 0; }
private:
void operator&() const; // Can't take address of nullptr
} nullptr = {};
operator T*() const
和operator T C::*() const
已在类中定义,因此可以自动内联。那么,为什么要再次添加inline
呢?- 为什么是
void operator&() const;
,而不是void operator&() = delete
? nullptr = {};
是什么意思?
最佳答案
添加额外的内联是样式问题。该说明符与链接和 ODR 违规有关,而不是实际内联。所有内联成员函数定义和模板成员函数通常都隐式具有该说明符。我认为 Scott Meyers 出于教学原因将其添加到此处。
Effective C++ 最初是为 C++03 编写的。那时还没有
= delete
。那时你只能声明但不定义函数。这是聚合初始化。
nullptr_t
的这个实现即使在 C++03 中也可以像这样初始化。它创建nullptr
的值。由于nullptr_t
没有用户提供的默认 c'tor,因此它是必需的。
关于c++ - Scott Meyers 第二版关于在 Effective C++ 中实现 nullptr 的一些问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46129024/