No, they cannot be different. For sufficiently different T1 and T2 , sizeof(T1 *) may differ from sizeof(T2 *) , but if T2 is just const T1 , then:
3.9.2 Connection types [basic.compound]
3 [...] Pointers to cv-qualifying and cv-unqualified versions (3.9.3) of types compatible with layouts should have the same requirements for the representation and alignment of values ββ(3.11). [...]
And any type T compatible with layouts with itself:
3.9 Types [basic.types]
11 If two types T1 and T2 are the same type, then T1 and T2 are layout compatible types. [...]
The representation of a value refers to the representation of an object; you cannot have the same representation of a value without having the same representation of an object. The latter means that the same number of bits is required.
3.9 Types [basic.types]
4 The object representation of an object of type T is a sequence of N unsigned char objects occupied by an object of type T , where N is equal to sizeof(T) . A representation of an object value is a collection of bits that contain a value of type T For trivially copied types, a value representation is a set of bits in an object representation that defines a value that is one discrete element of a particular set of values. 44
44). The C ++ memory model is assumed to be compatible with the ISO / IEC 9899 programming language model.
The point of demand, the reason is that it does not just say that the two types have the same representation of the object, is that T * and const T * have not only the same number of bits, but also the same bits in T * and const T * , which make up the value. This should guarantee not only that sizeof(T *) == sizeof(const T *) , but it means that you can use memcpy to copy the value of the T * pointer to the value of the const T * pointer or vice versa and get a meaningful result, the same the result will be obtained using const_cast .
Alignment requirements also provide some additional guarantees, but they are difficult to explain properly and are not directly related to this issue, and there are problems in the standard that undermine some of the implied guarantees, so I think itβs best to ignore it.
hvd Nov 23 '15 at 11:28 2015-11-23 11:28
source share