Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pointer interconvertible #77

Open
yumetodo opened this issue Nov 27, 2020 · 1 comment
Open

pointer interconvertible #77

yumetodo opened this issue Nov 27, 2020 · 1 comment

Comments

@yumetodo
Copy link
Contributor

yumetodo commented Nov 27, 2020

#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

@yumetodo
Copy link
Contributor Author

異なるオブジェクト型へのポインタ間のキャストは以下で定義されています。https://t.co/QqzA00iyhI
ここで現れる static_cast について、結果の型の有効なオブジェクトを指すようになる条件が pointer-interconvertible を使って定められています。https://t.co/9O5hRHfjnt

— Kazutoshi SATODA (@k-satoda) November 29, 2020

https://timsong-cpp.github.io/cppwp/n4861/expr.reinterpret.cast#7

An object pointer can be explicitly converted to an object pointer of a different type.64
When a prvalue v of object pointer type is converted to the object pointer type “pointer to cv T”, the result is static_­cast<cv T*>(static_­cast<cv void*>(v)).
[ Note: Converting a prvalue of type “pointer to T1” to the type “pointer to T2” (where T1 and T2 are object types and where the alignment requirements of T2 are no stricter than those of T1) and back to its original type yields the original pointer value.
— end note
]

  1. The types may have different cv-qualifiers, subject to the overall restriction that a reinterpret_­cast cannot cast away constness. ⮥

https://timsong-cpp.github.io/cppwp/n4861/expr.static.cast#13

A prvalue of type “pointer to cv1 void” can be converted to a prvalue of type “pointer to cv2 T”, where T is an object type and cv2 is the same cv-qualification as, or greater cv-qualification than, cv1.
If the original pointer value represents the address A of a byte in memory and A does not satisfy the alignment requirement of T, then the resulting pointer value is unspecified.
Otherwise, if the original pointer value points to an object a, and there is an object b of type T (ignoring cv-qualification) that is pointer-interconvertible with a, the result is a pointer to b.
Otherwise, the pointer value is unchanged by the conversion.
[ Example:

T* p1 = new T;
const T* p2 = static_cast<const T*>(static_cast<void*>(p1));
bool b = p1 == p2;  // b will have the value true.

— end example
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant