Sujet : Re: how cast works?
De : bc (at) *nospam* freeuk.com (Bart)
Groupes : comp.lang.cDate : 12. Aug 2024, 15:36:34
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v9d6li$3asjf$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9
User-Agent : Mozilla Thunderbird
On 09/08/2024 18:12, David Brown wrote:
On 09/08/2024 12:04, Bart wrote:
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.)
>
You've written it yourself. Both are conversions - one is implicit, the other is explicit.
Something you might observe here is that both lines are represented, despite the fact that the x's value is immediately overwritten after the first assignment, and its value is not used even after the second.
What follows was actually not shown, but it doesn't matter.
You presumably would use optimisation flags every time, so risking unpredictable chunks of the output disappearing depending on how early they cut in.
In fact, if I do this (a, x have int, double types):
a;
x;
my compiler produces some output, namely, evaluating and leaving results into a register:
mov eax, [rbp+`main.a]
movq XMM4, [rbp+`main.x]
gcc however produces no output even with -O0.
(I'd wanted to see what gcc did with an 'open' expression, one which would not be coerced into a particularly type, but those are rare in C code.)
Since C likes to use the term 'cast' for such conversions, I don't see a problem with talking about implicit and explicit versions.
>
C does not "like" to use the term "cast" for anything other than cast operations, as defined by the C standards. Implicit conversions are not casts.
/You/ might like to call implicit conversions "casts", but you'd be wrong to do so.
It just seems to irk the pedantics here.
You mean, people who know what they are talking about rather than those that make up stuff as they go along?
You yourself said that my own language is just C with a different syntax. Since I've worked on various implementations of it most of my life, is it possible that I might know what I'm talking about as well?
What I haven't done with my language is to create a reference document with its own precisely defined nomenclature, whose existence then requires that any casual discussion about ANY aspects of it to use exactly those terms and no others.