Groups | Search | Server Info | Keyboard shortcuts | Login | Register


Groups > comp.lang.c > #387461

Re: how cast works?

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: how cast works?
Date 2024-08-09 16:58 -0700
Organization None to speak of
Message-ID <87frrd5jw7.fsf@nosuchdomain.example.com> (permalink)
References (7 earlier) <v94pji$m1ib$1@dont-email.me> <v95lb7$26koh$1@dont-email.me> <20240809142622.467@kylheku.com> <87jzgp5php.fsf@nosuchdomain.example.com> <20240809152755.123@kylheku.com>

Show all headers | View raw


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.com
void Void(void) { Void(); } /* The recursive call of the void */

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-07 08:28 -0300
  Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-07 08:33 -0300
    Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-07 13:13 -0700
    Re: how cast works? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-11 17:43 -0700
      Re: how cast works? Vir Campestris <vir.campestris@invalid.invalid> - 2024-08-12 11:51 +0100
        Challenge/exercise problem - signum() function Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-12 08:17 -0700
          Re: Challenge/exercise problem - signum() function Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2024-08-12 16:07 +0000
            Re: Challenge/exercise problem - signum() function Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-12 09:57 -0700
  Re: how cast works? Dan Purgert <dan@djph.net> - 2024-08-07 20:00 +0000
    Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-07 13:26 -0700
    Re: how cast works? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-08-07 23:00 +0000
    Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-08 08:14 -0300
      Re: how cast works? Bart <bc@freeuk.com> - 2024-08-08 14:23 +0100
        Re: how cast works? Michael S <already5chosen@yahoo.com> - 2024-08-08 19:32 +0300
          Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-08 14:11 -0300
          Re: how cast works? Bart <bc@freeuk.com> - 2024-08-08 18:29 +0100
            Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-08 14:50 -0300
              Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-08 14:57 -0300
              Re: how cast works? Bart <bc@freeuk.com> - 2024-08-08 19:01 +0100
                Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-08 15:13 -0300
              Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-08 12:29 -0700
            Re: how cast works? David Brown <david.brown@hesbynett.no> - 2024-08-08 19:58 +0200
              Re: how cast works? Bart <bc@freeuk.com> - 2024-08-08 20:09 +0100
                Re: how cast works? David Brown <david.brown@hesbynett.no> - 2024-08-09 00:32 +0200
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-08 16:14 -0700
                Re: how cast works? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-08-09 02:47 +0000
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-08 22:55 -0700
                Re: how cast works? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-09 02:08 -0400
                Re: how cast works? David Brown <david.brown@hesbynett.no> - 2024-08-09 18:16 +0200
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 12:18 -0700
                Re: how cast works? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-11 17:07 -0700
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-11 20:14 -0700
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-09 01:56 +0100
                Re: how cast works? David Brown <david.brown@hesbynett.no> - 2024-08-09 19:08 +0200
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-10 11:03 +0100
                Re: how cast works? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-08-09 02:45 +0000
      Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-08 12:42 -0700
        Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-08 17:34 -0300
          Re: how cast works? Bart <bc@freeuk.com> - 2024-08-08 22:41 +0100
            Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-08 16:17 -0700
              Re: how cast works? Bart <bc@freeuk.com> - 2024-08-09 11:04 +0100
                Re: how cast works? David Brown <david.brown@hesbynett.no> - 2024-08-09 19:12 +0200
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-12 15:36 +0100
                Re: how cast works? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-09 13:57 -0400
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-09 21:59 +0100
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 14:47 -0700
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-10 00:32 +0100
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 17:12 -0700
                Re: how cast works? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-09 18:29 -0400
                Re: how cast works? David Brown <david.brown@hesbynett.no> - 2024-08-13 11:18 +0200
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-13 11:34 +0100
                Re: how cast works? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-13 07:51 -0400
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-13 14:01 +0100
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-13 12:46 -0700
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-13 21:51 +0100
                Re: how cast works? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-13 16:46 -0700
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-14 00:56 +0100
                Re: how cast works? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-18 03:37 -0700
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 14:29 -0700
                Re: how cast works? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-09 18:35 -0400
                Re: how cast works? Kaz Kylheku <643-408-1753@kylheku.com> - 2024-08-09 21:30 +0000
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 14:57 -0700
                Re: how cast works? Kaz Kylheku <643-408-1753@kylheku.com> - 2024-08-09 23:14 +0000
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 16:58 -0700
                Re: how cast works? Kaz Kylheku <643-408-1753@kylheku.com> - 2024-08-10 00:06 +0000
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 17:27 -0700
                Re: how cast works? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-09 20:31 -0400
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-10 01:11 +0100
                Re: how cast works? David Brown <david.brown@hesbynett.no> - 2024-08-13 11:23 +0200
                Re: how cast works? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-11 17:32 -0700
                Re: how cast works? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-09 18:35 -0400
                Re: how cast works? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-11 17:27 -0700
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 12:23 -0700
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-09 21:31 +0100
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 13:49 -0700
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-09 22:01 +0100
                Re: how cast works? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-12 00:33 -0700
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-12 12:21 +0100
              Re: how cast works? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-11 17:46 -0700
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-12 02:00 +0100
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-11 20:23 -0700
                Re: how cast works? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-11 20:37 -0700
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-11 21:33 -0700
                Re: how cast works? Ben Bacarisse <ben@bsb.me.uk> - 2024-08-12 16:57 +0100
                Re: how cast works? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-12 10:04 -0700
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-12 13:35 -0700
            Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-09 07:57 -0300
              Re: how cast works? Bart <bc@freeuk.com> - 2024-08-09 16:25 +0100
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 12:06 -0700
              Re: how cast works? David Brown <david.brown@hesbynett.no> - 2024-08-09 19:20 +0200
                Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-09 15:54 -0300
                Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-09 16:05 -0300
                Re: how cast works? David Brown <david.brown@hesbynett.no> - 2024-08-09 21:43 +0200
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 13:28 -0700
                Re: how cast works? David Brown <david.brown@hesbynett.no> - 2024-08-09 22:01 +0200
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-10 11:17 +0100
                Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-10 10:15 -0300
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-10 17:14 +0100
                Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-10 20:01 -0300
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-10 17:10 -0700
                Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-11 09:23 -0300
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-11 13:30 +0100
                Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-11 14:16 -0300
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-11 13:38 -0700
                Re: how cast works? Bart <bc@freeuk.com> - 2024-08-12 12:24 +0100
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 13:26 -0700
                Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-09 18:01 -0300
                Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 14:53 -0700
              Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-09 12:03 -0700
                Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-09 16:22 -0300
          Re: how cast works? David Brown <david.brown@hesbynett.no> - 2024-08-09 00:36 +0200
    Re: how cast works? Dan Purgert <dan@djph.net> - 2024-08-08 14:08 +0000
    Re: how cast works? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-08-09 02:42 +0000
  Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-07 13:08 -0700
    Re: how cast works? Thiago Adams <thiago.adams@gmail.com> - 2024-08-08 08:35 -0300
      Re: how cast works? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-08 12:39 -0700
      Re: how cast works? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-08 18:40 -0400
        Re: how cast works? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-11 17:15 -0700
    Is there an audio book version (Was: how cast works?) gazelle@shell.xmission.com (Kenny McCormack) - 2024-08-08 16:19 +0000
  Re: how cast works? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-08-07 23:03 +0000

csiph-web