Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #88967 > unrolled thread
| Started by | Muttley@dastardlyhq.com |
|---|---|
| First post | 2023-02-04 15:30 +0000 |
| Last post | 2023-02-22 06:36 -0800 |
| Articles | 17 on this page of 37 — 14 participants |
Back to article view | Back to comp.lang.c++
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++ Öö Tiib <ootiib@hot.ee> - 2023-02-06 03:27 -0800
Re: lvalue assignment difference between C and C++ Muttley@dastardlyhq.com - 2023-02-06 15:49 +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++ 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++ Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-02-07 07:50 -0800
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++ 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
Re: lvalue assignment difference between C and C++ Armando di Matteo <armando.dimatteo87@gmail.com> - 2023-02-21 09:15 -0800
Re: lvalue assignment difference between C and C++ Andrey Tarasevich <andreytarasevich@hotmail.com> - 2023-02-21 09:46 -0800
Re: lvalue assignment difference between C and C++ Muttley@dastardlyhq.com - 2023-02-22 09:29 +0000
Re: lvalue assignment difference between C and C++ Öö Tiib <ootiib@hot.ee> - 2023-02-22 01:46 -0800
Re: lvalue assignment difference between C and C++ Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-02-22 06:36 -0800
Page 2 of 2 — ← Prev page 1 [2]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2023-02-08 02:52 +0000 |
| Message-ID | <87v8kclvgu.fsf@bsb.me.uk> |
| In reply to | #89008 |
Muttley@dastardlyhq.com writes: > On Mon, 06 Feb 2023 13:03:11 -0800 > Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: <cut> >>C's grammar is not trivial to parse, due to typedefs. A typedef >>effectively creates a context-specific keyword, which means the parser >>has to interact with the symbol table. (Historically, this is because >>typedefs were introduced relatively late in the evolution of C.) > > Could it not cheat and simply treat a typedef as a kind of macro? Keith has mentioned the scope issue, but there are other problems with a simple textual substitution. For example, after typedef int *T; the declaration const T x; gives x a const-qualified type (i.e. you can't assign to x). A simple substitution of "T" for "int *" does not have the same effect at all. -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | Paavo Helde <eesnimi@osa.pri.ee> |
|---|---|
| Date | 2023-02-06 09:11 +0200 |
| Message-ID | <trq970$2vkp9$1@dont-email.me> |
| In reply to | #88969 |
04.02.2023 18:59 Muttley@dastardlyhq.com kirjutas:
> On Sat, 4 Feb 2023 11:04:12 -0500
> Richard Damon <Richard@Damon-Family.org> wrote:
>> On 2/4/23 10: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:
>>>
>>> #include <stdio.h>
>>>
>>> int main()
>>> {
>>> int a = 0;
>>> int b = 0;
>>> int x = 0;
>>> int y = 1;
>>> int c = 123;
>>>
>>> /* Compiles in C++, not C
>>> (x == y ? a : b) = c;
>>> */
>>>
>>> /* C version */
>>> *(x == y ? &a : &b) = c;
>>>
>>> printf("a = %d, b = %d\n",a,b);
>>> return 0;
>>> }
>>>
>>> Just curious.
>>>
>>
>> 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.
>>
>> By using the address-of and deference operatiors you convert that
>> r-value into an l-value so can make the assignment.
>>
>> In C++, because it has a "reference" type, if the last two terms of the
>> conditional operator are objects of a compatible type, then the type of
>> the result can be a reference to that type, an option not available in
>> C, and this allows its value to be an l-value.
>
> 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.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Date | 2023-02-06 01:01 -0800 |
| Message-ID | <87h6vzupzf.fsf@nosuchdomain.example.com> |
| In reply to | #88982 |
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]
| From | Richard Damon <Richard@Damon-Family.org> |
|---|---|
| Date | 2023-02-06 07:42 -0500 |
| Message-ID | <MA6EL.71816$jiuc.63797@fx44.iad> |
| In reply to | #88984 |
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]
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Date | 2023-02-06 12:59 -0800 |
| Message-ID | <87v8keecih.fsf@nosuchdomain.example.com> |
| In reply to | #88987 |
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]
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2023-02-07 07:44 +0100 |
| Message-ID | <trsru8$3fet8$1@dont-email.me> |
| In reply to | #88982 |
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]
| From | Paavo Helde <eesnimi@osa.pri.ee> |
|---|---|
| Date | 2023-02-07 09:18 +0200 |
| Message-ID | <trsu02$3ffbm$2@dont-email.me> |
| In reply to | #89005 |
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]
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
|---|---|
| Date | 2023-02-07 07:50 -0800 |
| Message-ID | <86edr1cw4y.fsf@linuxsc.com> |
| In reply to | #89007 |
Paavo Helde <eesnimi@osa.pri.ee> writes: > 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. Great analogy. I tip my hat to you sir. It's interesting to note that in his talk on cpp2, Herb Sutter has abandoned references as a separate concept, because they are too complicated. Instead what references are mostly used for has been replaced by parameter passing modes.
[toc] | [prev] | [next] | [standalone]
| From | Kaz Kylheku <864-117-4973@kylheku.com> |
|---|---|
| Date | 2023-02-06 22:21 +0000 |
| Message-ID | <20230206141733.165@kylheku.com> |
| In reply to | #88968 |
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]
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Date | 2023-02-06 14:43 -0800 |
| Message-ID | <87mt5qe7pc.fsf@nosuchdomain.example.com> |
| In reply to | #88997 |
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]
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2023-02-04 19:41 +0100 |
| Message-ID | <trm8q2$233hb$2@dont-email.me> |
| In reply to | #88967 |
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]
| From | Andrey Tarasevich <andreytarasevich@hotmail.com> |
|---|---|
| Date | 2023-02-10 16:19 -0800 |
| Message-ID | <ts6mv1$16gqf$1@dont-email.me> |
| In reply to | #88967 |
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] | [next] | [standalone]
| From | Armando di Matteo <armando.dimatteo87@gmail.com> |
|---|---|
| Date | 2023-02-21 09:15 -0800 |
| Message-ID | <5fe9d6e6-7ec8-4a9a-b06e-f15524c7e98bn@googlegroups.com> |
| In reply to | #89083 |
Andrey Tarasevich wrote: > int a, b = 1; > (a = b) = 2; > // Invalid in C, valid in C++ > // (and starting from C++11 the behavior is well-defined) What does it do, assign `b` to `a` and immediately afterwards assign 2 to `a`?
[toc] | [prev] | [next] | [standalone]
| From | Andrey Tarasevich <andreytarasevich@hotmail.com> |
|---|---|
| Date | 2023-02-21 09:46 -0800 |
| Message-ID | <tt302i$16poa$1@dont-email.me> |
| In reply to | #89190 |
On 02/21/23 9:15 AM, Armando di Matteo wrote: >> int a, b = 1; >> (a = b) = 2; >> // Invalid in C, valid in C++ >> // (and starting from C++11 the behavior is well-defined) > > What does it do, assign `b` to `a` and immediately afterwards assign 2 to `a`? Yes. -- Best regards, Andrey
[toc] | [prev] | [next] | [standalone]
| From | Muttley@dastardlyhq.com |
|---|---|
| Date | 2023-02-22 09:29 +0000 |
| Message-ID | <tt4n9p$1fpcj$1@dont-email.me> |
| In reply to | #89191 |
On Tue, 21 Feb 2023 09:46:56 -0800 Andrey Tarasevich <andreytarasevich@hotmail.com> wrote: >On 02/21/23 9:15 AM, Armando di Matteo wrote: >>> int a, b = 1; >>> (a = b) = 2; >>> // Invalid in C, valid in C++ >>> // (and starting from C++11 the behavior is well-defined) >> >> What does it do, assign `b` to `a` and immediately afterwards assign 2 to >`a`? > >Yes. Wouldn't the compiler optimise this for primitive types where there will be no side effects from the assignment and simply assign 2 to both a and b directly?
[toc] | [prev] | [next] | [standalone]
| From | Öö Tiib <ootiib@hot.ee> |
|---|---|
| Date | 2023-02-22 01:46 -0800 |
| Message-ID | <966c9549-b530-458a-af7a-cd1bbf72b34dn@googlegroups.com> |
| In reply to | #89200 |
On Wednesday, 22 February 2023 at 11:29:47 UTC+2, Mut...@dastardlyhq.com wrote: > On Tue, 21 Feb 2023 09:46:56 -0800 > Andrey Tarasevich <andreyta...@hotmail.com> wrote: > >On 02/21/23 9:15 AM, Armando di Matteo wrote: > >>> int a, b = 1; > >>> (a = b) = 2; > >>> // Invalid in C, valid in C++ > >>> // (and starting from C++11 the behavior is well-defined) > >> > >> What does it do, assign `b` to `a` and immediately afterwards assign 2 to > >`a`? > > > >Yes. > Wouldn't the compiler optimise this for primitive types where there will be > no side effects from the assignment and simply assign 2 to both a and b > directly? No. Compilers I tried (if set to optimise) seem to do it same way. If `a` and `b` are further used then use constants 2 as `a` and 1 as `b` there. If those are not used then optimise all the posted code out totally.
[toc] | [prev] | [next] | [standalone]
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
|---|---|
| Date | 2023-02-22 06:36 -0800 |
| Message-ID | <86o7plbwcs.fsf@linuxsc.com> |
| In reply to | #89083 |
Andrey Tarasevich <andreytarasevich@hotmail.com> writes:
> 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.
The dot operator ('.') propagates lvalueness.
A compound literal expression gives an lvalue result.
(Sorry if this posting seems misplaced. Given the original
context, comp.lang.c++ seemed like the best choice, even
though the remarks above don't involve C++ per se.)
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.c++
csiph-web