Sujet : Re: how cast works?
De : bc (at) *nospam* freeuk.com (Bart)
Groupes : comp.lang.cDate : 09. Aug 2024, 11:04:35
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v94pji$m1ib$1@dont-email.me>
References : 1 2 3 4 5 6 7
User-Agent : Mozilla Thunderbird
On 09/08/2024 00:17, Keith Thompson wrote:
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.
There is no such thing as an "implicit cast" in C.
Suppose I write this code:
x = a; // implicit 'conversion'
x = (double)a; // explicit 'conversion'
My compiler produces these two bits of AST for the RHS of both expressions:
1 00009 r64---|---2 convert: sfloat_c i32 => r64
1 00009 i32---|---|---1 name: t.main.a.1
1 00010 r64---|---2 convert: sfloat_c i32 => r64
1 00010 i32---|---|---1 name: t.main.a.1
So whatever you call that `(double)` part of the second line, which is written explicitly, exactly the same thing is done internally (ie 'implicitly') to the first line. (The 09/10 are line numbers.)
Since C likes to use the term 'cast' for such conversions, I don't see a problem with talking about implicit and explicit versions.
It just seems to irk the pedantics here.