🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #1325

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

#1325
From: Kaz Kylheku
Date: Thu, 05 Nov 2026 01:35
42 lines
2808 bytes
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,
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 += 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 += 2) *= 3) -= 5) %= 11;  // impossible in C, possible in C++

Does that work for a basic type like int, and is it well-defined?

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.


-- 
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
Technical Details
Message-ID: <20261104171507.263@kylheku.com>
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>

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: Kaz Kylheku <046-301-5902@kylheku.com>
Newsgroups: comp.lang.c
Subject: Re: Rationales for result type of operations using operators like
 '+='
Date: Thu, 5 Nov 2026 01:35:06 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <20261104171507.263@kylheku.com>
References: <11ceoa2$85p3$3@dont-email.me>
Injection-Date: Thu, 05 Nov 2026 01:35:07 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1261564"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/ytY28+fjcdsb+PeM4ZcIOoa4RNscJWmM="; posting-host="c65f12b943b6f58d246ae9ea3a9a1592"
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:uesRjmg8+R1D91HoxqL+kFbAayY= sha256:tZ/0aku1ON6aVe3e9wMWUOfpfQP2xwrPZ94ppW6OViE=
	sha1:/94wQECfSQ+/f5JqMY1lNhnZ4PU= sha256:CeE4RxbfnCcegVrdJO5aIhOgXbZJxFmGT2gxsa//yL8=