Kaz Kylheku <
643-408-1753@kylheku.com> writes:
On 2024-08-09, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
Kaz Kylheku <643-408-1753@kylheku.com> writes:
[...]
It almost makes sense to speak of "implicit cast" (i.e. coercion) in C,
because of what happens implicitly being so unsafe.
>
I disagree, because that's not what "cast" means.
>
"cast" means to (try to) project a value into another type.
*Looks around* sorry, are we still in comp.lang.c?
"Cast" has a number of meanings in contexts outside C, applicable to
dice, eyes, fishing lines, ballots, magic spells, actors, liquid metal,
and broken limbs, among other things. In C, it means what the standard
says it means, even if some people misuse it to refer to implicit
conversions.
In C though, the nuance is something like "conversion that is mediated
by the presence of the cast notation", where "mediated" includes the
possibility that the cast notation has no effect at all
(e.g. 2 + (int) 3).
I hadn't noticed before that the standard does have a formal definition
of the term "cast", as well as "explicit conversion" and "implicit
conversion".
Quoting N3220, and using *...* to denote italics (definitions) (earlier
editions are identical as far as I can tell) :
6.3p1 :
Several operators convert operand values from one type to another
automatically. This subclause specifies the result required from
such an *implicit conversion*, as well as those that result from a
cast operation (an *explicit conversion*).
p2 :
Unless explicitly stated otherwise, conversion of an operand value
to a compatible type causes no change to the value or the
representation.
(I don't know of anything that explicitly states otherwise.)
6.5.5p6 :
Preceding an expression by a parenthesized type name converts the
value of the expression to the unqualified, non-atomic version of
the named type. This construction is called a *cast*. A cast that
specifies no conversion has no effect on the type or value of an
expression.
The syntax for a cast-expression is :
cast-expression :
unary-expression
( type-name ) cast-expression
The term "cast" refers to a cast-expression that matches the second
alternative.
The "no conversion" wording is odd. Most likely the intent is that
casting an expression to its own type, like `(int)3`, "specifies no
conversion". This is supported by the statement in 6.3: "Several
operators convert operand values from one type **to another**
automatically." (emphasis added).
My own preference would be to say that a conversion of an expression to
its own type is still a conversion, but a trivial one.
If it's really the case that not every cast specifies a conversion, then
defining a "cast" as an "explicit conversion" is not quite correct.
That is IMHO be unfortunate. It's also inconsistent with the statement
two sentences earlier in the same paragraph, which says unconditionally
that a cast converts the value.
In any case, while there may be some ambiguity about whether all casts
specify conversions, it is unambiguous that an implicit conversion is
not a cast.
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */