nullptr 是如何定义的?
How is nullptr defined?
在 Effective Modern C++、"Iterm 8: Prefer nullptr to 0 and NULL." 第 59 页,它说:
nullptr
's actual type is std::nullptr_t
, and, in a wonderfully circular definition, std::nullptr_t
is defined to be the type of nullptr
.
一个奇妙的循环定义?
这是怎么来的?
简而言之,nullptr
是一个可以赋给任何类型指针的值,在布尔上下文中它是假的(不像大多数指针是 new
/malloc
,或引用一个有效的对象),尽管它本身是一个有效的指针,但取消引用它会导致 UB,就像 in/decrementing 它一样,它是单例类型的唯一值 nullptr_t
。
像这样。
在 Effective Modern C++、"Iterm 8: Prefer nullptr to 0 and NULL." 第 59 页,它说:
nullptr
's actual type isstd::nullptr_t
, and, in a wonderfully circular definition,std::nullptr_t
is defined to be the type ofnullptr
.
一个奇妙的循环定义?
这是怎么来的?
简而言之,nullptr
是一个可以赋给任何类型指针的值,在布尔上下文中它是假的(不像大多数指针是 new
/malloc
,或引用一个有效的对象),尽管它本身是一个有效的指针,但取消引用它会导致 UB,就像 in/decrementing 它一样,它是单例类型的唯一值 nullptr_t
。
像这样。