static_cast'd pointer value

前端 未结 2 752
故里飘歌
故里飘歌 2021-01-04 21:06

In the current draft standard (and C++17), this is written about static_casting a void *:

A prvalue of type “pointer to cv1 void

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-04 21:53

    Is there a case, when casting a void * to something pointer-interconvertible actually changes the pointer value?

    No, the standard clearly states

    If two objects are pointer-interconvertible, then they have the same address

    What is the intent of this distinction?

    I would think the distinction comes not in the value of the pointer but in it's semantics. The important part is that you end up with a pointer to the object b (which inter-covertible with the original object pointed). This is a stronger and more specific guarantee than "the original value remains unchanged", even though in this case the original value also remains unchanged.

    Stronger guarantee: the resulting pointer points to an actual object (not just to an address in memory)

    A more specific guarantee: this object is specified: it is the object b which is pointer interchangeable with the original object pointed by the pointer.

提交回复
热议问题