In accordance with this
http://eel.is/c++draft/basic.compound#4
Two objects a and b are mutually reversible for pointers if:
- they are the same object or
- one is a union object and the other is a non-static data member of this object ([class.union]) or
- one is an object of the standard layout class, and the second is the first non-static data member of this object or, if the object does not have non-static data members, the first sub-object of the base class of this object ([class .mem]) or
- there is an object c such that a and c are mutually invertible for pointers, and c and b are mutually invertible for pointers.
If two objects are mutually convertible, then they have the same address, and you can get a pointer to one of the pointers to the other via reinterpret_cast. [ Note. The array object and its first element are not mutually convertible with a pointer, although they have the same address. - final note]
Why does the C ++ standard not guarantee mutual convertibility between an array of objects and its first element, while guaranteeing a class and its first member?
How does it look like undefined?
char carr[8]; char& ch0 = carr[0]; auto& carr2 = reinterpret_cat<char (&) [8]>(ch0); // Is this considered undefined behavior? // Because ch0 (char&) and carr2 (char(&)[8]) are not "pointer-interconvertible". // So they can not use reinterpret_cast "definedly". // Array and its first element are not "pointer-interconvertible" // eventhough they share the same address.
So far this is an example on the standard itself.
http://www.eel.is/c++draft/basic.types#2
#define N sizeof(T) char buf[N]; T obj;
Think if T = char [N]? Well, won't & obj become char (&) [N]? Why is std :: memcpy so excellent that it can use char (&) [N] while we cannot.
This is so funny.
I really want to know why the standard intentionally makes the char * and std::memcpy tools only for iterating memory by bytes.
source share