Using std::launder to “validate” non “pointer to object” pointer value since C++17

前端 未结 1 1983
夕颜
夕颜 2021-02-05 06:57

According to this answer, since C++17, even if a pointer has the right address and the right type dereferencing it can cause undefined behaviour.

 align         


        
1条回答
  •  借酒劲吻你
    2021-02-05 07:30

    No. The bytes constituting the int object p2 points to are not reachable through p1+1.


    The "reachable" rule basically means that launder doesn't allow you to access storage you can't legally access via the original pointer. Since an opaque function may launder pointers as much as it wants, permitting this kind of shenanigans would substantially inhibit escape analysis.

    0 讨论(0)
提交回复
热议问题