nullptr,{}和nullopt之间的区别 [英] Difference between nullptr, {} and nullopt

查看:313
本文介绍了nullptr,{}和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 to T, it is a value that is either a T or is nothing. You cannot have a "dangling optional", 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 any optional type.

      {}是(在期望值的上下文中)表示 默认构造的对象,类型为T",其中T是从上下文中推断出的某种类型.如果T是指针类型,则它将等于nullptr.如果Toptional类型,它将比较等于nullopt.如果T是算术类型,它将比较等于0.如果T是标准容器类型之一,则将等于该容器中的其他空容器类型.

      {} is (in a context expecting a value) an expression that means "a default constructed object of type T", where T is some type inferred from the context. If T is a pointer type, it will compare equal to nullptr. If T is an optional type, it will compare equal to nullopt. If T is an arithmetic type, it will compare equal to 0. If T is one of the standard container types, it will compare equal to other empty containers of that type.

      这篇关于nullptr,{}和nullopt之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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