Why can't you take the address of nullptr?

后端 未结 4 1146
野性不改
野性不改 2021-02-05 00:39

In the C++11 standard, I don\'t understand the reason why taking the address of nullptr is disallowed whereas one is allowed to take the address of their own st

4条回答
  •  独厮守ぢ
    2021-02-05 00:42

    If you're after a standard answer, § 18.2/9 puts your observations pretty bluntly:

    Although nullptr’s address cannot be taken, the address of another nullptr_t object that is an lvalue can be taken.

    Alternatively, § 2.14.7 says this about nullptr:

    The pointer literal is the keyword nullptr. It is a prvalue of type std::nullptr_t.

    So what is a prvalue? § 3.10/1 answers that:

    A prvalue (“pure” rvalue) is an rvalue that is not an xvalue. [ Example: The result of calling a function whose return type is not a reference is a prvalue. The value of a literal such as 12, 7.3e5, or true is also a prvalue. — end example ]

    Hopefully, trying to take the address of any of those things in the example will make more sense as to why you can't take the address of nullptr. It's part of those examples!

提交回复
热议问题