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


Groups > comp.lang.c > #169181 > unrolled thread

lvalue assignment difference between C and C++

Started byMuttley@dastardlyhq.com
First post2023-02-04 15:30 +0000
Last post2023-02-10 16:19 -0800
Articles 15 on this page of 35 — 13 participants

Back to article view | Back to comp.lang.c


Contents

  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

Page 2 of 2 — ← Prev page 1 [2]


#169197

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2023-02-06 01:01 -0800
Message-ID<87h6vzupzf.fsf@nosuchdomain.example.com>
In reply to#169196
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.

-- 
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 */

[toc] | [prev] | [next] | [standalone]


#169200

FromRichard Damon <Richard@Damon-Family.org>
Date2023-02-06 07:42 -0500
Message-ID<MA6EL.71816$jiuc.63797@fx44.iad>
In reply to#169197
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.

[toc] | [prev] | [next] | [standalone]


#169206

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2023-02-06 12:59 -0800
Message-ID<87v8keecih.fsf@nosuchdomain.example.com>
In reply to#169200
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 */

[toc] | [prev] | [next] | [standalone]


#169203

Frombart c <bart4858@gmail.com>
Date2023-02-06 08:11 -0800
Message-ID<44c37621-b302-472d-a48f-a6c6239ce5b7n@googlegroups.com>
In reply to#169196
On Monday, 6 February 2023 at 07:11:43 UTC, Paavo Helde wrote:

> 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.

C already has to deal differently with constructs like this to allow them to be lvalues:

    A = x;
    *A = x;
    A[i] = x;
    A.m = x

Allowing:

    c ? A : B = x;

is not much different, and little extra cognitive loads. In this last example, A and B can be any of the forms in the first four examples, plus nested ?: terms.

The only thing that is somewhat different, is that the two lvalue expressions (or more with nested ?:) need to have a more closely compatible type than when they are r-values. However, if a compiler simply transforms this into:

    *(c ? &A : &B) = x;

then this is taken care of as normal.

[toc] | [prev] | [next] | [standalone]


#169208

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2023-02-06 21:19 +0000
Message-ID<87ilgemqyu.fsf@bsb.me.uk>
In reply to#169203
bart c <bart4858@gmail.com> writes:

> On Monday, 6 February 2023 at 07:11:43 UTC, Paavo Helde wrote:
>
>> 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.
>
> C already has to deal differently with constructs like this to allow
> them to be lvalues:
>
>     A = x;
>     *A = x;
>     A[i] = x;
>     A.m = x
>
> Allowing:
>
>     c ? A : B = x;

Just to add an often overlooked detail...  In C, what you wrote does
parse as you intended: (c ? A : B) = x so you'll be told that = needs an
lvalue on the left, but in C++ it parses as c ? A : (B = x) so you need
to use ()s assign to the result of a conditional expression in C++.

> is not much different, and little extra cognitive loads. In this last
> example, A and B can be any of the forms in the first four examples,
> plus nested ?: terms.
>
> The only thing that is somewhat different, is that the two lvalue
> expressions (or more with nested ?:) need to have a more closely
> compatible type than when they are r-values. However, if a compiler
> simply transforms this into:
>
>     *(c ? &A : &B) = x;
>
> then this is taken care of as normal.

C could choose to define c ? A : B = x to mean *(c ? &A : &B) = x but
there would be odd corner cases such as A or B being bit fields or
declared with storage class 'register'.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#169211

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-02-07 07:44 +0100
Message-ID<trsru8$3fet8$1@dont-email.me>
In reply to#169196
Am 06.02.2023 um 08:11 schrieb Paavo Helde:

> 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.

References are easy to learn.

[toc] | [prev] | [next] | [standalone]


#169212

FromPaavo Helde <eesnimi@osa.pri.ee>
Date2023-02-07 09:18 +0200
Message-ID<trsu02$3ffbm$2@dont-email.me>
In reply to#169211
07.02.2023 08:44 Bonita Montero kirjutas:
> Am 06.02.2023 um 08:11 schrieb Paavo Helde:
> 
>> 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.
> 
> References are easy to learn.

Yes, in the same way one push-up is easy to do.

[toc] | [prev] | [next] | [standalone]


#169209

FromKaz Kylheku <864-117-4973@kylheku.com>
Date2023-02-06 22:21 +0000
Message-ID<20230206141733.165@kylheku.com>
In reply to#169182
On 2023-02-04, Richard Damon <Richard@Damon-Family.org> wrote:
> The key difference is that in C, the results of the conditional operator 
> is always an r-value, and thus not suitable for an assignment operator.

Interestingly, unlike C++, ISO C doesn't use the term rvalue (only
lvalue). However: the term was used in the reference manual for the B
language, predecessor to C.

A footnote (I'm looking at C99, where it is note #53 on P. 46) says
"What is sometimes called ‘‘rvalue’’ is in this International Standard
described as the ‘‘value of an expression’’."

Every regular here knows this trivia; just dredging it up a bit here.

-- 
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

[toc] | [prev] | [next] | [standalone]


#169210

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2023-02-06 14:43 -0800
Message-ID<87mt5qe7pc.fsf@nosuchdomain.example.com>
In reply to#169209
Kaz Kylheku <864-117-4973@kylheku.com> writes:
> On 2023-02-04, Richard Damon <Richard@Damon-Family.org> wrote:
>> The key difference is that in C, the results of the conditional operator 
>> is always an r-value, and thus not suitable for an assignment operator.
>
> Interestingly, unlike C++, ISO C doesn't use the term rvalue (only
> lvalue). However: the term was used in the reference manual for the B
> language, predecessor to C.
>
> A footnote (I'm looking at C99, where it is note #53 on P. 46) says
> "What is sometimes called ‘‘rvalue’’ is in this International Standard
> described as the ‘‘value of an expression’’."
>
> Every regular here knows this trivia; just dredging it up a bit here.

Right, and the way C (non-normatively) defines "rvalue" is a bit odd.
An lvalue is a kind of expression, but an rvalue is the result of
evaluating an expression.

Historically, lvalues and rvalues were kinds of values.  An expression
could be "evaluated for its lvalue" or "evaluated for its rvalue",
depending on the context in which it appears.  In particular, the left
hand side of an assignment is evaluated for its lvalue (the identify of
the object it designates), and the right hand side of an assignment is
evaluated for its rvalue.  Some expressions, such as 42, have rvalues
but not lvalues; thus 42 cannot appear on the LHS of an assignment.
This probably goes back to BCPL, or perhaps even CPL.

C mostly dropped rvalues and redefined an "lvalue" as a kind of
expression.

C++ has a small menagerie of kinds of rvalues and lvalues, all of which
(I think) are kinds of expressions.

I think I prefer the historical meanings, but we're stuck with what the
standards say now.  (It took the ISO C standard three tries to define
"lvalue" consistently, so I don't propose rocking that particular boat.)

-- 
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 */

[toc] | [prev] | [next] | [standalone]


#169216

FromPaul N <gw7rib@aol.com>
Date2023-02-07 03:17 -0800
Message-ID<0f9840b3-7421-47ae-a507-0069cbfed700n@googlegroups.com>
In reply to#169210
On Monday, February 6, 2023 at 10:43:27 PM UTC, Keith Thompson wrote:
> Kaz Kylheku <864-11...@kylheku.com> writes: 
> > On 2023-02-04, Richard Damon <Ric...@Damon-Family.org> wrote: 
> >> The key difference is that in C, the results of the conditional operator 
> >> is always an r-value, and thus not suitable for an assignment operator. 
> > 
> > Interestingly, unlike C++, ISO C doesn't use the term rvalue (only 
> > lvalue). However: the term was used in the reference manual for the B 
> > language, predecessor to C. 
> > 
> > A footnote (I'm looking at C99, where it is note #53 on P. 46) says 
> > "What is sometimes called ‘‘rvalue’’ is in this International Standard 
> > described as the ‘‘value of an expression’’." 
> > 
> > Every regular here knows this trivia; just dredging it up a bit here.
> Right, and the way C (non-normatively) defines "rvalue" is a bit odd. 
> An lvalue is a kind of expression, but an rvalue is the result of 
> evaluating an expression. 
> 
> Historically, lvalues and rvalues were kinds of values. An expression 
> could be "evaluated for its lvalue" or "evaluated for its rvalue", 
> depending on the context in which it appears. In particular, the left 
> hand side of an assignment is evaluated for its lvalue (the identify of 
> the object it designates), and the right hand side of an assignment is 
> evaluated for its rvalue. Some expressions, such as 42, have rvalues 
> but not lvalues; thus 42 cannot appear on the LHS of an assignment. 
> This probably goes back to BCPL, or perhaps even CPL. 

Yes, that is how it worked in BCPL. In fact, BCPL has keywords LV and RV which can be used to evaluate an expression in its other context, but the symbols @ and ! can also do this - I guess the symbols were added later and people used them instead, leading to C's @ and *.

[toc] | [prev] | [next] | [standalone]


#169219

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2023-02-07 16:25 +0000
Message-ID<871qn1mohw.fsf@bsb.me.uk>
In reply to#169216
Paul N <gw7rib@aol.com> writes:

> On Monday, February 6, 2023 at 10:43:27 PM UTC, Keith Thompson wrote:
>> Kaz Kylheku <864-11...@kylheku.com> writes: 
>> > On 2023-02-04, Richard Damon <Ric...@Damon-Family.org> wrote: 
>> >> The key difference is that in C, the results of the conditional operator 
>> >> is always an r-value, and thus not suitable for an assignment operator. 
>> > 
>> > Interestingly, unlike C++, ISO C doesn't use the term rvalue (only 
>> > lvalue). However: the term was used in the reference manual for the B 
>> > language, predecessor to C. 
>> > 
>> > A footnote (I'm looking at C99, where it is note #53 on P. 46) says 
>> > "What is sometimes called ‘‘rvalue’’ is in this International Standard 
>> > described as the ‘‘value of an expression’’." 
>> > 
>> > Every regular here knows this trivia; just dredging it up a bit here.
>> Right, and the way C (non-normatively) defines "rvalue" is a bit odd. 
>> An lvalue is a kind of expression, but an rvalue is the result of 
>> evaluating an expression. 
>> 
>> Historically, lvalues and rvalues were kinds of values. An expression 
>> could be "evaluated for its lvalue" or "evaluated for its rvalue", 
>> depending on the context in which it appears. In particular, the left 
>> hand side of an assignment is evaluated for its lvalue (the identify of 
>> the object it designates), and the right hand side of an assignment is 
>> evaluated for its rvalue. Some expressions, such as 42, have rvalues 
>> but not lvalues; thus 42 cannot appear on the LHS of an assignment. 
>> This probably goes back to BCPL, or perhaps even CPL. 
>
> Yes, that is how it worked in BCPL. In fact, BCPL has keywords LV and
> RV which can be used to evaluate an expression in its other context,

That was very early BCPL.  By the time the book was published (and when
I first used it) all references to rvalues and lvalues had gone!  I
think the very detailed distinction made early on was probably a
hangover from CPL and was subsequently found to be unnecessarily fussy.

> but the symbols @ and ! can also do this - I guess the symbols were
> added later and people used them instead,

The early manual (with _lv_ and _rv_ operators) is of the old-school
type where the syntax is given using presentation symbols (often
underlined) that will map to some concrete representation in a
particular implementation.  I never used BCPL that old, so I don't know
what was used for _rv_ and _lv_ in practice.  Other underlined symbols
were usually written in upper case so I'd guess RV and LV.  Did you use
(or even see) BCPL of the era?  Though off-topic, I'd love to see some!

I'm not 100% sure that the later @ (essentially "address-of") and unary
! (essentially "dereference") are exactly what the 1967 manual writes as
_rv_ and _lv_.  There are enough other differences in that early version
(and the example code at the end is sadly missing) for it to be unclear,
at least to me.  Do you know if they were, in fact, just concrete
representations of those keywords?

> leading to C's @ and *.

You mean C's & and *.  In the later BCPL, ! was also binary with a!b
being, essentially, C's a[b].  Early BCPL (which is the form that
influenced B and C) used the very clumsy a*[b] form because []s were
also used for function calls.  That syntax, sin[pi], definitely comes
from CPL.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#169224

FromPaul N <gw7rib@aol.com>
Date2023-02-08 13:19 -0800
Message-ID<9f807ac3-fe63-4caa-ac7d-a8d1c849620an@googlegroups.com>
In reply to#169219
On Tuesday, February 7, 2023 at 4:25:44 PM UTC, Ben Bacarisse wrote:
> Paul N <gw7...@aol.com> writes: 
> 
> > On Monday, February 6, 2023 at 10:43:27 PM UTC, Keith Thompson wrote: 
> >> Right, and the way C (non-normatively) defines "rvalue" is a bit odd. 
> >> An lvalue is a kind of expression, but an rvalue is the result of 
> >> evaluating an expression. 
> >> 
> >> Historically, lvalues and rvalues were kinds of values. An expression 
> >> could be "evaluated for its lvalue" or "evaluated for its rvalue", 
> >> depending on the context in which it appears. In particular, the left 
> >> hand side of an assignment is evaluated for its lvalue (the identify of 
> >> the object it designates), and the right hand side of an assignment is 
> >> evaluated for its rvalue. Some expressions, such as 42, have rvalues 
> >> but not lvalues; thus 42 cannot appear on the LHS of an assignment. 
> >> This probably goes back to BCPL, or perhaps even CPL. 
> > 
> > Yes, that is how it worked in BCPL. In fact, BCPL has keywords LV and 
> > RV which can be used to evaluate an expression in its other context,
> That was very early BCPL. By the time the book was published (and when 
> I first used it) all references to rvalues and lvalues had gone! I 
> think the very detailed distinction made early on was probably a 
> hangover from CPL and was subsequently found to be unnecessarily fussy.

I got my information from two books. One, entitled "Programming electronic switching systems" and dated 1976, briefly discusses various programming languages and mentions the two types of evaluation as a key feature of the language, together with keywords RV and LV. It describes an array access as either RV (E1 + E2) or as E1 ↓ E2 (a downward arrow). It's possible the authors had not kept up with recent developments in the language, which is but one of over a dozen described.

The other book is "BCPL: The language and its compiler", dated 1980, printed 1986. As you say, there appears to be no mention of lvalues or rvalues, and no discussion of LV and RV, but the latter are shown in the sample code for the language's syntax analyser. LV seems identical to @ but RV and ! are different as the latter can also be used for arrays, which are now E1 ! E2 as you say below.

> > but the symbols @ and ! can also do this - I guess the symbols were 
> > added later and people used them instead,
> The early manual (with _lv_ and _rv_ operators) is of the old-school 
> type where the syntax is given using presentation symbols (often 
> underlined) that will map to some concrete representation in a 
> particular implementation. I never used BCPL that old, so I don't know 
> what was used for _rv_ and _lv_ in practice. Other underlined symbols 
> were usually written in upper case so I'd guess RV and LV. Did you use 
> (or even see) BCPL of the era? Though off-topic, I'd love to see some! 

I don't think there are any underlines of any sort in the book. Full stops seem to be the preferred character for concatenating words into a single identifier.

> I'm not 100% sure that the later @ (essentially "address-of") and unary 
> ! (essentially "dereference") are exactly what the 1967 manual writes as 
> _rv_ and _lv_. There are enough other differences in that early version 
> (and the example code at the end is sadly missing) for it to be unclear, 
> at least to me. Do you know if they were, in fact, just concrete 
> representations of those keywords?

As I said, the older book certainly seems to say that the keywords were simply RV and LV, in capitals.

> > leading to C's @ and *.
> You mean C's & and *.

Mea culpa!

> In the later BCPL, ! was also binary with a!b 
> being, essentially, C's a[b]. Early BCPL (which is the form that 
> influenced B and C) used the very clumsy a*[b] form because []s were 
> also used for function calls. That syntax, sin[pi], definitely comes 
> from CPL. 

Yes, both round and square brackets are function calls.

Glad to find someone who is interested!

[toc] | [prev] | [next] | [standalone]


#169226

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2023-02-08 23:46 +0000
Message-ID<87wn4rk9ff.fsf@bsb.me.uk>
In reply to#169224
Paul N <gw7rib@aol.com> writes:

> On Tuesday, February 7, 2023 at 4:25:44 PM UTC, Ben Bacarisse wrote:
>> Paul N <gw7...@aol.com> writes: 
>> 
>> > On Monday, February 6, 2023 at 10:43:27 PM UTC, Keith Thompson wrote: 
>> >> Right, and the way C (non-normatively) defines "rvalue" is a bit odd. 
>> >> An lvalue is a kind of expression, but an rvalue is the result of 
>> >> evaluating an expression. 
>> >> 
>> >> Historically, lvalues and rvalues were kinds of values. An expression 
>> >> could be "evaluated for its lvalue" or "evaluated for its rvalue", 
>> >> depending on the context in which it appears. In particular, the left 
>> >> hand side of an assignment is evaluated for its lvalue (the identify of 
>> >> the object it designates), and the right hand side of an assignment is 
>> >> evaluated for its rvalue. Some expressions, such as 42, have rvalues 
>> >> but not lvalues; thus 42 cannot appear on the LHS of an assignment. 
>> >> This probably goes back to BCPL, or perhaps even CPL. 
>> > 
>> > Yes, that is how it worked in BCPL. In fact, BCPL has keywords LV and 
>> > RV which can be used to evaluate an expression in its other context,
>> That was very early BCPL. By the time the book was published (and when 
>> I first used it) all references to rvalues and lvalues had gone! I 
>> think the very detailed distinction made early on was probably a 
>> hangover from CPL and was subsequently found to be unnecessarily fussy.
>
> I got my information from two books. One, entitled "Programming
> electronic switching systems" and dated 1976, briefly discusses
> various programming languages and mentions the two types of evaluation
> as a key feature of the language, together with keywords RV and LV. It
> describes an array access as either RV (E1 + E2) or as E1 ↓ E2 (a
> downward arrow). It's possible the authors had not kept up with recent
> developments in the language, which is but one of over a dozen
> described.
>
> The other book is "BCPL: The language and its compiler", dated 1980,
> printed 1986. As you say, there appears to be no mention of lvalues or
> rvalues, and no discussion of LV and RV, but the latter are shown in
> the sample code for the language's syntax analyser. LV seems identical
> to @ but RV and ! are different as the latter can also be used for
> arrays, which are now E1 ! E2 as you say below.

I've got that one (well, had that one would be more accurate) and it
describes what I would call "modern BCPL", if anything from last century
can be modern!

Binary ! is a separate thing.  I was wondering if rv and lv are exactly
unary ! and @ in modern BCPL.  I need to find more examples of rv and lv
being used in code to be sure.

>> > but the symbols @ and ! can also do this - I guess the symbols were 
>> > added later and people used them instead,
>>
>> The early manual (with _lv_ and _rv_ operators) is of the old-school 
>> type where the syntax is given using presentation symbols (often 
>> underlined) that will map to some concrete representation in a 
>> particular implementation. I never used BCPL that old, so I don't know 
>> what was used for _rv_ and _lv_ in practice. Other underlined symbols 
>> were usually written in upper case so I'd guess RV and LV. Did you use 
>> (or even see) BCPL of the era? Though off-topic, I'd love to see some! 
>
> I don't think there are any underlines of any sort in the book.

I used Usenet _underline_ notation for putting an line under a word.
Some clients will _underline_ and make *bold* and /italic/ text using _,
* and / characters.  I think that ended up confusing the matter.

And I was referring only the very old reference manuals.  They were
typically typed with a mechanical typewriter and used underlining to
denote abstract symbols like rv, if, let etc..  In such a notation, a
keyword like let would be underlined and the reader would not
automatically assume that a variable could not be named let.  An
implementation would specify how these symbols should really be written
in actual program texts.  In the BCPL I used, keywords were written in
UPPER CASE, so you could have

  LET let = 42; // not a good idea

Algol 68 used a similar system with bold keywords that could be
represented in several ways.  Upper case was one, but sometimes you
could use quotes or single dots: WHILE, 'while', .while were all seen
where the report used bold while.  (This was called stropping, but I
don't know why.  A metaphorical 'sharpening'?)

I think this convention had died out by the 1980s.

> Full stops seem to be the preferred character for concatenating words
> into a single identifier.

Yes, I think that's always been the case.  I was not talking about
that.  My post was confusing.

>> I'm not 100% sure that the later @ (essentially "address-of") and unary 
>> ! (essentially "dereference") are exactly what the 1967 manual writes as 
>> _rv_ and _lv_. There are enough other differences in that early version 
>> (and the example code at the end is sadly missing) for it to be unclear, 
>> at least to me. Do you know if they were, in fact, just concrete 
>> representations of those keywords?
>
> As I said, the older book certainly seems to say that the keywords
> were simply RV and LV, in capitals.
>
>> > leading to C's @ and *.
>> You mean C's & and *.
>
> Mea culpa!
>
>> In the later BCPL, ! was also binary with a!b 
>> being, essentially, C's a[b]. Early BCPL (which is the form that 
>> influenced B and C) used the very clumsy a*[b] form because []s were 
>> also used for function calls. That syntax, sin[pi], definitely comes 
>> from CPL. 
>
> Yes, both round and square brackets are function calls.
>
> Glad to find someone who is interested!

Thanks for posting.  I'd forgotten all about the original RV and LV
symbols.

I was very fond of programming in BCPL and I think a fast, clean
typeless language would still find place these days.  It's and odd
fondness since I also like very strict typing as in, say, Haskell.  Have
you had a look at Richard's MCPL?  Very interesting...

And, as an aside, somewhere along the line BCPL acquired a%b the byte
offset version of a!b.  You might remember that string operations were
rather painful without it.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#169185

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-02-04 19:41 +0100
Message-ID<trm8q2$233hb$2@dont-email.me>
In reply to#169181
Am 04.02.2023 um 16:30 schrieb Muttley@dastardlyhq.com:

>          (x == y ? a : b) = c;

I'm sometimes writing code like this and I love it.
If you use it regulary this becomes more readbale
than the if'd code.

[toc] | [prev] | [next] | [standalone]


#169234

FromAndrey Tarasevich <andreytarasevich@hotmail.com>
Date2023-02-10 16:19 -0800
Message-ID<ts6mv1$16gqf$1@dont-email.me>
In reply to#169181
On 02/04/23 7:30 AM, Muttley@dastardlyhq.com wrote:
> Can anyone explain the "official" explanation from whichever standard as to
> why the first assignment only compiles in C++ but not C:

Because C is not C++, and C++ is not C.

This is one of the fundamental core differences between the languages, 
which is present in all potentially applicable contexts. You just 
stumbled upon one of them - the `?`: operator. There are others:

* Assignment (and compound assignment) in C++ preserves lvalue-ness of 
the result

   int a, b = 1;
   (a = b) = 2;
   // Invalid in C, valid in C++
   // (and starting from C++11 the behavior is well-defined)

* Prefix increment and decrement in C++ preserves lvalue-ness of the result

   int i = 0;
   ++(++i);
   // Invalid in C, valid in C++
   // (and starting from C++11 the behavior is well-defined)

C language from the very beginning never cared about preserving the 
lvalue-ness of expression results. In C dereference operator (unary `*`) 
and its direct derivatives (e.g. `[]` and `->`) are the only operators 
that produce lvalue results. Everything else produces rvalues.

C++ completely abandoned and reworked that approach. C++ does the 
opposite: it goes to great lengths in order to preserve lvalue-ness of 
the result whenever possible. You can see it especially well with `?:` 
operator where lvalue-preservation is obviously tricky, since the types 
(and value categories) of the second and third operands might not match. 
The behavior of `?:` in C++ is defined by a long set of rather 
convoluted rules, which depend on how well the types and categories of 
the second and third operands match. This illustrates how much effort 
C++ is willing to spend on preserving something that C just discarded 
nonchalantly.

On can possibly argue that the rationale behind this is introduction of 
reference types in C++ (and I'd say that references is just small part 
of the story), but the bottom line here is that C and C++ are languages 
that adhere to diametrically opposite philosophies wrt that core issue. 
These are two completely different languages. The matter can only seem 
surprising to those who believe in silly myths, like "C is subset of 
C++" and such.

-- 
Best regards,
Andrey

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.c


csiph-web