简体   繁体   中英

prvalue of type std::nullptr_t

Section 4.10/1 N3797 says:

A null pointer constant is an integer literal (2.14.2) with value zero or a prvalue of type std::nullptr_t.

I think nullptr is a prvalue of type std::nullptr_t . Would you get a bit more examples of such prvalue ?

As with any type, there are various ways to get a prvalue of type std::nullptr_t :

  • cast from an appropriate expression: static_cast<std::nullptr_t>(0)
  • temporary constructor call: std::nullptr_t{}
  • call to a function returning std::nullptr_t : std::nullptr_t f() { return {}; } f() std::nullptr_t f() { return {}; } f()
  • call to a lambda returning std::nullptr_t : []() -> std::nullptr_t { return {}; }() []() -> std::nullptr_t { return {}; }()
  • a comma expression where the right hand side is a std::nullptr_t prvalue: ("hello", nullptr)
  • a conditional expression where one side has type std::nullptr_t and the other side is of a different value category or is a throw-expression: false ? throw "oops" : nullptr false ? throw "oops" : nullptr , false ? std::move(nullptr) : nullptr false ? std::move(nullptr) : nullptr

Because std::nullptr_t does not participate in most operators, this is a reasonably exhaustative list; most other expressions with type std::nullptr_t will yield glvalues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM