A strict smoothing rule is defined elsewhere. This is the wording:
C (ISO / IEC 9899: 1999 6.5 / 7):
The object must have a stored value, accessible only with the value of the lvalue expression, which has one of the following types:
- a type compatible with an efficient object type,
- qualified version of a type compatible with an efficient type of object,
- a type that is a signed or unsigned type corresponding to the effective type of the object,
- a type that is a signed or unsigned type corresponding to a qualified version of an effective object type,
- an aggregate or union type that includes one of the above types among its members (including, recursively, a subaggregate or contains union member) or
- character type.
C ++ (ISO / IEC 14882: 2011 3.10 [basicl.lval] / 15):
If the program tries to access the stored value of the object through the l-value of another, except for one of the following types, the behavior is undefined:
- dynamic type of object
- cv-qualified version of the dynamic type of an object,
- a type similar (as defined in 4.4) for the dynamic type of an object,
- a type that is a signed or unsigned type corresponding to a dynamic type of an object,
- a type that is a signed or unsigned type corresponding to a cv-qualified version of a dynamic object type,
- a type of aggregate or union that includes one of the above types among its elements or non-static data elements (including, recursively, an element or non-static summation data element or contains a union),
- a type that is (possibly cv-qualified) a base class of the type dynamic object type,
- a
char or unsigned char .
The C standard does not prevent you from pointing to a pointer to an unrelated type, provided there are no binding problems. However, due to the strict rule of aliases, you basically cannot dereference a pointer received from such an actor. Therefore, the only useful thing associated with such an “invalid” pointer is to return it to the correct type (or compatible type).
This is basically the same in C ++ with reinterpret_cast (5.2.10 [expr.reinterpret.cast] / 7):
An object pointer can be explicitly converted to an object pointer of another type. When prvalue v type "pointer to T1 " is converted to type "pointer to cv T2 ", the result is static_cast<cv T2*>(static_cast<cv void*>(v)) if both T1 and T2 are standard layout types (3.9 ), and the alignment requirements of T2 no more stringent than the requirements for T1 , or if one of the types is void . Converting the pointer to T1 pointer type to the pointer to T2 type (where T1 and T2 are object types and where the alignment requirements of T2 no more stringent than the requirements of T1 ) and back to the original type, gives the initial value of the pointer. The result of any other such pointer conversion is not specified.
source share