🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #1330

Re: Rationales for result type of operations using operators like '+='

#1330
From: Janis Papanagnou
Date: Thu, 05 Nov 2026 02:36
63 lines
3622 bytes
On 2026-11-05 02:35, Kaz Kylheku wrote:
> On 2026-11-04, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>> I just stumbled about something in "C" where I knew there was behavior
>> that other languages designed differently; specifically the "compound"
>> "and-becomes" operators (+=, -=, *=, /=, %=).
>>
>> I'm basically looking for substantial[*] rationales for two properties;
>> (a) that their result is an rvalue (and not an lvalue), and
>> (b) that their evaluation in cascades has right-associativity.
> 
> These properties make sense for the plain assignment operator,

Yes.

> for supporting x = y = z = 0. If this expression denoted a single
> lvalue, that being x, then y and z would not receive any value,
> and would have to be initialized: first x would be assigned the value
> of y, then it would be assigned the value of z, and then zero.

> The operational assignment operators were given the same properties as
> plain assignment, without any thought of doing something usefully
> different.

I suspected that.

> 
>>     i += 2 *= 3 -= 5 %= 11;        // impossible in C and C++
> 
> But
> 
>   i += j += k *= 3
> 
> /is/ possible. k gets multiplied by 3, then its new value is added
> to j, and the new value of j is added to i.

I see two problems; thinking an expression "reverse", and not being
able to use integer literals (instead of variables). - In practice I'd
then prefer the other workaround, like decomposition of the expression
in separate statements.

With parentheses we at least see that the first part of the expression
returns an lvalue (and can potentially be used at the outer stages of
the expression).

> 
>>     (((i += 2) *= 3) -= 5) %= 11;  // impossible in C, possible in C++
> 
> Does that work for a basic type like int, and is it well-defined?

Erm.. - isn't it? - I certainly have thought it is. And I see no reason
for any surprise here. Do you?

> 
> Possibly, under modern C++, a "sequenced before" requirement rescues
> it, but I suspect that if we go back to C++98, it would have been
> undefined: i is modified multiple times without an intervening sequence
> point.

Hmm.. - I'm sure I used that pattern during the early 1990's. - Are you
saying I was just lucky back then?

Janis
Technical Details
Message-ID: <11cgq30$85p3$6@dont-email.me>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <11ceoa2$85p3$3@dont-email.me> <20261104171507.263@kylheku.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: Janis Papanagnou <janis_papanagnou+ng@hotmail.com>
Newsgroups: comp.lang.c
Subject: Re: Rationales for result type of operations using operators like
 '+='
Date: Thu, 5 Nov 2026 03:36:16 +0100
Organization: A noiseless patient Spider
Lines: 63
Message-ID: <11cgq30$85p3$6@dont-email.me>
References: <11ceoa2$85p3$3@dont-email.me> <20261104171507.263@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 05 Nov 2026 02:36:16 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="268067"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18KgMuafedARWlDxTFbbEor"; posting-host="bb4bc76d4e37a83d4ff8352de8d1e679"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:CZavL3WUzIIUkygQ+xbfn92d4Lo= sha256:FlptMdzbK1quLB+VI0ofqTrIwVsuAFBQzzqh1bpmOV4=
	sha1:RHM8qKY3MZ4te6TeBVSMv7UsPko= sha256:Zk2PHGL8zYzhGiMoROKWINizre08FqHnR5oP7aSJKd4=
Content-Language: en-US
In-Reply-To: <20261104171507.263@kylheku.com>