Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: how cast works? Date: Fri, 09 Aug 2024 16:58:00 -0700 Organization: None to speak of Lines: 81 Message-ID: <87frrd5jw7.fsf@nosuchdomain.example.com> References: <87ttfu94yv.fsf@nosuchdomain.example.com> <87bk228uzg.fsf@nosuchdomain.example.com> <20240809142622.467@kylheku.com> <87jzgp5php.fsf@nosuchdomain.example.com> <20240809152755.123@kylheku.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Sat, 10 Aug 2024 01:58:05 +0200 (CEST) Injection-Info: dont-email.me; posting-host="0c353c12c58454f0df934f7da485585f"; logging-data="269016"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Uqdl2/tmmde391Rhu4hLb" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:1sEYnkqglGTwHwn2dwpuNb4lmEs= sha1:jX7ZuP7l4J2QnZk5DRUTv3/p7ps= Xref: csiph.com comp.lang.c:387461 Kaz Kylheku <643-408-1753@kylheku.com> writes: > On 2024-08-09, Keith Thompson 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.com void Void(void) { Void(); } /* The recursive call of the void */