Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #169206

Re: lvalue assignment difference between C and C++

Path csiph.com!weretis.net!feeder8.news.weretis.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c++, comp.lang.c
Subject Re: lvalue assignment difference between C and C++
Date Mon, 06 Feb 2023 12:59:18 -0800
Organization None to speak of
Lines 84
Message-ID <87v8keecih.fsf@nosuchdomain.example.com> (permalink)
References <trltmg$21cft$1@dont-email.me> <0mvDL.68810$b7Kc.34465@fx39.iad> <trm2tb$224hr$1@dont-email.me> <trq970$2vkp9$1@dont-email.me> <87h6vzupzf.fsf@nosuchdomain.example.com> <MA6EL.71816$jiuc.63797@fx44.iad>
MIME-Version 1.0
Content-Type text/plain
Injection-Info reader01.eternal-september.org; posting-host="181ea74528ed435ea473c00f282e1c24"; logging-data="3366995"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/icDT+Ivfnx+I7Sg8NcFuc"
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock sha1:jI4mSSD20G3QORkWXm9gPDVPwag= sha1:d6YrgvZKwPKdNSqIck7YTSOGu9k=
Xref csiph.com comp.lang.c++:88993 comp.lang.c:169206

Cross-posted to 2 groups.

Show key headers only | View raw


Richard Damon <Richard@Damon-Family.org> writes:
> On 2/6/23 4:01 AM, Keith Thompson wrote:
>> Paavo Helde <eesnimi@osa.pri.ee> writes:
>>> 04.02.2023 18:59 Muttley@dastardlyhq.com kirjutas:
>> [...]
>>>> Ok, that makes sense. Though I wonder - given the main C compilers
>>>> used now
>>>> are also C++ compilers - why they don't just silently allow it in C? Would
>>>> there be any wierd or unfortunate side effects in C if it was?
>>>
>>> This would basically introduce a limited notion of 'reference' in the
>>> C language, which would make the language more complex to teach and
>>> learn. That's the opposite to the goals C currently has.
>>>
>>> And yes, having a simpler language in general means the programs
>>> written in it need to be more complex. Here it just means a couple of
>>> extra asterisks and ampersands, so not a big deal.
>> I agree it's not a big deal, and I've already commented on the idea
>> that most C compilers are also C++ compilers (they aren't).
>> But I don't think that allowing a conditional expression to be an
>> lvalue would require introducing references into the language.
>> An lvalue is defined as "an expression that potentially designated
>> an object" ("potentially" because *ptr is still an lvalue even if
>> ptr==NULL).  This would just mean that `x ? y : z` would designate an
>> object, and the identify of that object depends on the value of `x`.
>> It's not all that much different from `a[i]`, where the identify
>> of the object it designates depends on the values of `a` and `i`
>> -- or `*p` for that matter.
>> If it were introduced in a future edition of the C standard, I don't
>> think it would cause many problems.  (And the generated machine code
>> would probably use pointers anyway.)  But I don't think there's enough
>> demand to justify it.
>
> I think the limitation is that to parse:
>
>  int a, b, c, d;
>  ...
>  (a ? b : c) = d;
>
> The "value" used from b, for parsing the expression isn't the integral
> value of b, but something like a int& that will convert to the value
> if needed. The whole design phylosophy of C is that while parse the 
> statement, you can evalutate the pieces largly ignoring remote
> context. The remembering that you have an l-value never has to survive
> going through an "expression" but always (currently) used just by the 
> immediate operator that consumes it.
>
> To implement this, b's l-valueness (if c is also an l-value) through
> the conditional operator to see how that is being used. Since
> conditionals can be nested, this requires a somewhat arbitrary level
> of delay.
>
> C++ get around that by b evaluating not to the value of b, but a
> reference to b, that switches to the value if used as an r-value or 
> stays an l-value if used as an assignment target.
>
> Thus the ability comes straight out of having a "reference" type.

You make a good point that I hadn't thought of: that a conditional
expression would have to be an lvalue or not depending whether b and c
are both lvalues (and there would have to be tighter constraints on the
types of b and c in that case).  And that would violate the (probably
not quite universal) rule that every C (sub-)expression is evaluated
without regard to its context.

The C++ standard does define this in terms of "lvalue references".

But I think this could be defined in a future edition of C without using
C++-style references, probably by saying something like:

    A conditional expression is an lvalue if and only if the second
    and third operands are lvalues of compatible types.

(it would probably require substantially more words than that).
I don't think it would be worth the effort, but I wouldn't complain
if the C committee and implementers decided to do it.

(I'll note that a parenthesized lvalue is an lvalue, but of course
that's much more trival than a conditional expression.)

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for XCOM Labs
void Void(void) { Void(); } /* The recursive call of the void */

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


Thread

lvalue assignment difference between C and C++ Muttley@dastardlyhq.com - 2023-02-04 15:30 +0000
  Re: lvalue assignment difference between C and C++ Richard Damon <Richard@Damon-Family.org> - 2023-02-04 11:04 -0500
    Re: lvalue assignment difference between C and C++ Muttley@dastardlyhq.com - 2023-02-04 16:59 +0000
      Re: lvalue assignment difference between C and C++ Richard Damon <Richard@Damon-Family.org> - 2023-02-04 13:02 -0500
      Re: lvalue assignment difference between C and C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-04 12:45 -0800
      Re: lvalue assignment difference between C and C++ David Brown <david.brown@hesbynett.no> - 2023-02-05 11:42 +0100
        Re: lvalue assignment difference between C and C++ Muttley@dastardlyhq.com - 2023-02-06 09:26 +0000
          Re: lvalue assignment difference between C and C++ David Brown <david.brown@hesbynett.no> - 2023-02-06 14:13 +0100
            Re: lvalue assignment difference between C and C++ Muttley@dastardlyhq.com - 2023-02-06 15:50 +0000
              Re: lvalue assignment difference between C and C++ David Brown <david.brown@hesbynett.no> - 2023-02-06 20:08 +0100
              Re: lvalue assignment difference between C and C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-06 12:47 -0800
          Re: lvalue assignment difference between C and C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-06 13:03 -0800
            Re: lvalue assignment difference between C and C++ Muttley@dastardlyhq.com - 2023-02-07 09:24 +0000
              Re: lvalue assignment difference between C and C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-07 11:08 -0800
                Re: lvalue assignment difference between C and C++ Muttley@dastardlyhq.com - 2023-02-08 09:23 +0000
                Re: lvalue assignment difference between C and C++ Blue-Maned_Hawk <bluemanedhawk@gmail.com> - 2023-02-08 22:13 -0500
                Re: lvalue assignment difference between C and C++ James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-02-08 22:53 -0500
                Re: lvalue assignment difference between C and C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-08 22:12 -0800
              Re: lvalue assignment difference between C and C++ Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-02-08 02:52 +0000
      Re: lvalue assignment difference between C and C++ Paavo Helde <eesnimi@osa.pri.ee> - 2023-02-06 09:11 +0200
        Re: lvalue assignment difference between C and C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-06 01:01 -0800
          Re: lvalue assignment difference between C and C++ Richard Damon <Richard@Damon-Family.org> - 2023-02-06 07:42 -0500
            Re: lvalue assignment difference between C and C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-06 12:59 -0800
        Re: lvalue assignment difference between C and C++ bart c <bart4858@gmail.com> - 2023-02-06 08:11 -0800
          Re: lvalue assignment difference between C and C++ Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-02-06 21:19 +0000
        Re: lvalue assignment difference between C and C++ Bonita Montero <Bonita.Montero@gmail.com> - 2023-02-07 07:44 +0100
          Re: lvalue assignment difference between C and C++ Paavo Helde <eesnimi@osa.pri.ee> - 2023-02-07 09:18 +0200
    Re: lvalue assignment difference between C and C++ Kaz Kylheku <864-117-4973@kylheku.com> - 2023-02-06 22:21 +0000
      Re: lvalue assignment difference between C and C++ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-06 14:43 -0800
        Re: lvalue assignment difference between C and C++ Paul N <gw7rib@aol.com> - 2023-02-07 03:17 -0800
          Re: lvalue assignment difference between C and C++ Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-02-07 16:25 +0000
            Re: lvalue assignment difference between C and C++ Paul N <gw7rib@aol.com> - 2023-02-08 13:19 -0800
              Re: lvalue assignment difference between C and C++ Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-02-08 23:46 +0000
  Re: lvalue assignment difference between C and C++ Bonita Montero <Bonita.Montero@gmail.com> - 2023-02-04 19:41 +0100
  Re: lvalue assignment difference between C and C++ Andrey Tarasevich <andreytarasevich@hotmail.com> - 2023-02-10 16:19 -0800

csiph-web