Why can't you take the address of nullptr?

后端 未结 4 1141
野性不改
野性不改 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:58

    nullptr is a (literal) constant, and these don't have a memory address, like any other literal constant in your code. It's similar to 0, but of the special std::nullptr_t type instead of void* to avoid problems with overloading (pointers vs. integers).

    But if you define your own variable with the value nullptr, it has a memory address, so you can take its address.

    The same holds for any other literal constant (which in C++ fall under the category prvalue) of any other type, since literal constants aren't stored in your program (only as parts of expressions where they occur), that's why it doesn't make any sense to talk about addresses. However, constant variables do have addresses, to point out the difference.

提交回复
热议问题