Skip to content

pointer interconvertible #77

@yumetodo

Description

@yumetodo

#67 ではStrict Aliasing Ruleについて論じたが、以下のキャストはそもそもpointer interconvertibleではないからStrict Aliasing Rule以前の問題だとする指摘がなされた。

class A {};
class B {};

A a;
B* b = reinterpret_cast<B*>(&a);

そもそも元スレのreinterpret_cast式は現在のWDによるといわゆるstrict aliasing ruleで定義されてません。pointer interconvertibleを満たさない場合には意味がすでに必要とされてないため、文面通りの「未定義行為」です。

— g_naggnoyil(gint, gchar **) (@gnaggnoyil) November 27, 2020

https://timsong-cpp.github.io/cppwp/n4861/basic.compound#4.4
4 Two objects a and b are pointer-interconvertible if:

(4.1)
they are the same object, or
(4.2)
one is a union object and the other is a non-static data member of that object ([class.union]), or
(4.3)
one is a standard-layout class object and the other is the first non-static data member of that object, or, if the object has no non-static data members, any base class subobject of that object ([class.mem]), or
(4.4)
there exists an object c such that a and c are pointer-interconvertible, and c and b are pointer-interconvertible.

If two objects are pointer-interconvertible, then they have the same address, and it is possible to obtain a pointer to one from a pointer to the other via a reinterpret_­cast.
[ Note: An array object and its first element are not pointer-interconvertible, even though they have the same address.
— end note
]

cc: @onihusube @yohhoy

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions