Date: Sun, 25 Aug 2019 00:35:11 +0300
On 21/08/2019 22:31, language.lawyer_at_[hidden] wrote:
> On 21/08/2019 22:11, Timur Doumler wrote:
>> This doesn't make sense to me.
>
> That's why some people think this should be fixed.
>
>> If I have an object of type int (or, say, a struct containing an int), then the value has 4 bytes. If I access that value by dereferencing a char pointer that aliases that object, all I can ever get from that char is a single byte. How can this be "the object"?
>
> The standard just says what result you get, it is an implementation's problem to answer the "how" question.
> However, http://eel.is/c++draft/expr.pre#4 could help some implementations:
>> If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined.
>
> If you have an int object and try to access its value through a char lvalue and the value stored in the int object is not in the range representable by the char type, the behavior is undefined.
>
> [expr.pre]/4 helps little-endian machine implementations, because they just can fetch the first byte of the int object and this would give the right value when there is no UB.
However, there is no analog of [expr.pre]/4 for assignment.
In the code
int x = WHATEVER;
*reinterpret_cast<char*>(&x) = 0;
the value of `x` shall become `0`.
> On 21/08/2019 22:11, Timur Doumler wrote:
>> This doesn't make sense to me.
>
> That's why some people think this should be fixed.
>
>> If I have an object of type int (or, say, a struct containing an int), then the value has 4 bytes. If I access that value by dereferencing a char pointer that aliases that object, all I can ever get from that char is a single byte. How can this be "the object"?
>
> The standard just says what result you get, it is an implementation's problem to answer the "how" question.
> However, http://eel.is/c++draft/expr.pre#4 could help some implementations:
>> If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined.
>
> If you have an int object and try to access its value through a char lvalue and the value stored in the int object is not in the range representable by the char type, the behavior is undefined.
>
> [expr.pre]/4 helps little-endian machine implementations, because they just can fetch the first byte of the int object and this would give the right value when there is no UB.
However, there is no analog of [expr.pre]/4 for assignment.
In the code
int x = WHATEVER;
*reinterpret_cast<char*>(&x) = 0;
the value of `x` shall become `0`.
Received on 2019-08-24 16:37:18