prvalue of type std::nullptr_t

prvalue of type std::nullptr_t

本文关键字:nullptr std of type prvalue      更新时间:2023-10-16

第4.10/1 N3797节说:

空指针常量是值为零的整数文字(2.14.2(或std::nullptr_t类型的prvalue。

我认为nullptrstd::nullptr_t类型的prvalue。你能得到更多这样的prvalue的例子吗?

与任何类型一样,有多种方法可以获得类型为std::nullptr_t:的prvalue

  • 从适当的表达式强制转换:static_cast<std::nullptr_t>(0)
  • 临时构造函数调用:std::nullptr_t{}
  • 调用返回std::nullptr_t:std::nullptr_t f() { return {}; } f()的函数
  • 调用返回std::nullptr_t:[]() -> std::nullptr_t { return {}; }()的lambda
  • 右侧为std::nullptr_t的逗号表达式prvalue:("hello", nullptr)
  • 一个条件表达式,其中一侧具有类型std::nullptr_t,另一侧具有不同的值类别,或者是抛出表达式:false ? throw "oops" : nullptrfalse ? std::move(nullptr) : nullptr

由于std::nullptr_t不参与大多数运算符,因此这是一个相当详尽的列表;类型为CCD_ 18的大多数其他表达式将产生glvalues。