nullptr,{}和nullopt之间的区别 [英] Difference between nullptr, {} and nullopt
问题描述
为什么我不能使用nullptr
代替nullopt
?为什么{}
像nullopt
一样翻译?
Why can't i use nullptr
instead of nullopt
? And why {}
translates like nullopt
?
推荐答案
-
optional<T>
不是指向T
的指针,它是一个T
或什么都不是.您不能像指针和引用那样具有悬挂optional
"或任何其他类型的无效"值.optional<T>
is not kind of pointer toT
, it is a value that is either aT
or is nothing. You cannot have a "danglingoptional
", or any other kind of "invalid" value, like you can with pointers and references.nullptr
是一个可以转换为任何指针类型的值的对象.nullptr
is an object that can be converted to a value of any pointer type.nullopt
是可以转换为任何optional
类型的值的对象.nullopt
is an object that can be converted to a value of anyoptional
type.{}
是(在期望值的上下文中)表示 默认构造的对象,类型为T
",其中T
是从上下文中推断出的某种类型.如果T
是指针类型,则它将等于nullptr
.如果T
是optional
类型,它将比较等于nullopt
.如果T
是算术类型,它将比较等于0
.如果T
是标准容器类型之一,则将等于该容器中的其他空容器类型.{}
is (in a context expecting a value) an expression that means "a default constructed object of typeT
", whereT
is some type inferred from the context. IfT
is a pointer type, it will compare equal tonullptr
. IfT
is anoptional
type, it will compare equal tonullopt
. IfT
is an arithmetic type, it will compare equal to0
. IfT
is one of the standard container types, it will compare equal to other empty containers of that type.这篇关于nullptr,{}和nullopt之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!