Sujet : Re: how cast works?
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 12. Aug 2024, 04:23:20
Autres entêtes
Organisation : None to speak of
Message-ID : <87le12e85z.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5 6 7 8 9
User-Agent : Gnus/5.13 (Gnus v5.13)
Bart <
bc@freeuk.com> writes:
On 12/08/2024 01:46, Tim Rentsch wrote:
Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
Bart <bc@freeuk.com> writes:
[...]
>
Take:
>
int a; double x;
>
x = (double)a;
>
The cast is implicit here but I've written it out to make it clear.
>
[...]
>
The *conversion* could be done implicitly, but you've used a cast (i.e.,
an explicit conversion) to make it clear.
The statement assigning to x performs two conversions: an explicit
one caused by the cast, and an implicit one caused by the assignment
operation.
>
The 'x' term is the other side of the cast from the 'a' term.
>
So after '(double)a' has been evaluated, both sides of '=' have the
type 'double', so no further conversion is needed.
Bart, I'm sure you don't care about this, but others might. Please do
us all a favor and don't argue about it.
The standard says that "In *simple assignment* (=), the value of the
right operand is converted to the type of the assignment expression and
replaces the value stored in the object designated by the left operand."
This is unambiguous, and for your example it means that the result of
the evaluating the RHS is *converted* from double to double.
"Conversion of an operand value to a compatible type causes no change to
the value or the representation." double is compatible with itself, so
you presumably won't find any evidence of a conversion if you examine
the generated code.
The standard could have described this differently, perhaps by saying
that no conversion is performed if the LHS and RHS have the same type,
but it says what it says.
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */