Article View: comp.lang.c
Article #1351Re: Rationales for result type of operations using operators like '+='
From: David Brown
Date: Fri, 06 Nov 2026 09:36
Date: Fri, 06 Nov 2026 09:36
358 lines
17259 bytes
17259 bytes
(I'm snipping a bit for brevity.) On 06/11/2026 01:19, Janis Papanagnou wrote: > On 2026-11-05 11:33, David Brown wrote: >> On 05/11/2026 03:19, Janis Papanagnou wrote: >>> Thanks for your answer. (I'll keep all your explanations for context.) >>> My comments below... >>> >>> On 2026-11-04 10:56, David Brown wrote: >>>> On 04/11/2026 08:53, Janis Papanagnou 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. >>>>> >>>> >>>> I don't see a distinction here between compound assignment and >>>> simple assignment - the compound assignment operators follow the >>>> simple assignment operator for consistency, and because "x += y" is >>>> defined to mean "x = x + y". So I think that we can just talk about >>>> simple assignment (or at least, talk about it first). >>> >>> Okay, I see where you're coming from. - Actually that reasoning is one >>> of the couple rationales that the AI provided me as answer as well. >>> >>> Since I don't see the need to not have a distinction between = and op= >>> I dismissed that. In consequence, as you say or imply, all you derive >>> from that is thus also not convincing to me. - Let me expand on that... >> >> I can agree that op= does not /need/ to be consistent with simple >> assignment for associativity and rvalue/lvalue results, but I think a >> language designer would need very strong reasons before allowing them >> to be inconsistent. > > You may have missed that I don't consider it "inconsistent". (The > reasons have already been explained.) No, I did not. But I did think more about repeated use of the same op=, rather than mixes of different op's. > > (I understood that with your _equalization_ of the = and op= classes > of operators you find it "inconsistent". As an opinion I accept it.) Sure. It's all just opinions here, not facts. I am not suggesting that I think my opinions are universal in any sense. It may be that the founding fathers of C shared some of my opinions and that influenced their design decisions - but I do not know anything specific there. >> >>> Given that cascading is considered useful and also used in practice I'd >>> like to consider that from another perspective; from use-cases. >> >> Cascading is also considered unclear and questionable style, with >> poorly defined semantics in special cases (such as volatile accesses) >> or if there are other side effects involved. > > Here you are speaking about 'volatile' in "C", right? - I've not > engaged with 'volatile' deeply enough to comment on that. - I'm > only asking whether that's a problem with the 'volatile' concept > or the op= logic as it's defined. (And whether 'volatile' and the > op= question is the only problem here - i.e. the "bad feature" is > the op= - where 'volatile' won't show any other problems with all > the other "C" constructs.) "volatile" turns up a lot in my field, but is not nearly as common for most C and C++ programmers. (Well, ultimately it /is/ common, or your programs would have little or no "observable behaviour", but it is deeply hidden within the libraries, system calls, etc.) C++ has deprecated many uses of "volatile", including its use in compound assignments, precisely because the semantics were unclear and often inconsistently implemented (even within the same compiler!). The point of using "volatile" variables is that each read and each write should be fully determined and ordered, which is difficult with complex expressions like those under discussion. > Concerning _side-effects_ is a general > problem in "C"; bart has in a recent post made up a side-effect > sample (to criticize op= , not to criticize writing side-effects). > I would definitely be unhappy with cascaded assignment (simple or compound) where there were side-effects of any kind. My point was just that while it is true that "cascading is considered useful" by /some/ people - such as yourself - it is also considered a bad idea by other people (such as me). These are both subjective opinions, and I don't think there is any kind of consensus. >> >> Different people will have different opinions on what should be >> allowed or disallowed in a language - and when they are willing to >> disallow some useful cases to ensure dangerous cases are compile-time >> errors, or when they are willing to allow some dangerous cases to >> ensure that useful cases are allowed. > > (Well, sure. That's why I asked for [sensible] use-cases.) > You are the one who uses these cascades, and who writes expressions in C++ that use the lvalue nature of the result of assignment operators. To me, with my preferences and style of coding, there are /no/ sensible use-cases. There may be cases that other people see as sensible. >> >> However the balance is picked for a language, it is inevitable that >> some potentially useful cases will be disallowed in order to reduce >> the risk of subtle errors in code. > > (Still not obvious what that actually would be. - No offense intended > but sounds more like FUD. That's why I asked for something concrete.) It was a general statement, but it might not have been helpful here. > > I can't see how op= expressions with rvalues and right-associativity > semantics would in any way be [significantly] more "risky" than the > C-form with lvalues and right associativity, or the C++-form with > rvalues and right-associativity. (It's C++ that has lvalue results, and C that has rvalue results - I think you have mixed up your rvalue's and lvalue's here. But I know what you meant.) Look at it from a different viewpoint. If C's assignment operators (simple and/or compound) evaluated to lvalues, what could you write that you cannot do with rvalues? "a += b *= c". Is that "risky" ? I think it would easily be misunderstood - though of course these things depend on how often you see such code. > >> >>> >>> What use-cases are there that would make it sensible to allow (in "C") >>> just rvalues, to prevent which misuses, or prevent what ambiguities, or >>> which problems with compilers, or would introduce any other problem? >> >> Perhaps my imagination is lacking. > > Mine as well. - That's why I was asking; if we'd have a convincing > example it could answer my original question. (So I can meanwhile > just speculate that it might be the reason that also Kaz mentioned > in his post.) If we do /not/ have a convincing example, then that is actually better progress towards answering your original question. If evaluating to an lvalue allows people to write very questionably code, but does not allow significant good code, then that is a good reason to say the evaluation is an rvalue. (I am not at all convinced by Kaz's suggestion that the decisions were made "without any thought". I do not believe the C designers and/or standardisers thought of all possible scenarios or code styles, but I would be surprised if they did not make the rvalue/lvalue decision actively. And given that C++ actively picked lvalue, the C committee could easily have seen that and changed C to match without affecting existing valid code, if they thought it was a good idea.) >> >> The only circumstances I can think of where "(a += b) += c" would be >> preferable to "a += (b + c)", is if the calculation order affected >> signed integer overflow or rounding in floating point. And then I >> think the code would be far better written as "a = (a + b) + c;". > > I'm not sure what the sample with '+' and '+=' serves here. (I'd have > thought my example with mixed operations would fit better explaining > things; it was: number *= base += digit; .) > Fair enough. But I would have the same reasoning for "(a *= b) + c". > WRT "a = (a + b) + c;" I'd just repeat my key-words: duplication and > parentheses. In "C" you have anyway only the choices already mentioned, > so just use what's existing. > >> >>> >>> What use-cases are there that would make it sensible to right-associate >>> (as in "C" and C++) modifications of an lvalue. >> >> In C++, functions can return references that are lvalues. Operators >> can be overloaded for user types, as functions. It is therefore >> consistent in the language that the result of assignment operators is >> an lvalue. But I don't see use-cases for lvalue results of assignment >> operators on standard types. > > I see the additional advantages for C++. One plausible use-case I see for C++ is when operators are used for an entirely different purpose. For example, "cout << a << b;" has nothing to do with shifts. If you wanted a right-associative operator, you could use "b >>= a >>= stack". This would require "a >>= stack" to be an lvalue. Of course, this applies to user-defined types and operator overloads, and does not require rvalue results from standard type assignment operators. > > And I see use-cases for lvalues in "C". (See above.) I don't see "number *= base += digit;" as a useful use-case in C or C++. I see it as a rather poor way to write code. I don't see saving a duplication of an identifier or a set of parentheses as an advantage. But I appreciate that /you/ do. > >> >>> >>> (Please keep in mind that there's yet no substantial need identified >>> to consider the two syntactic elements similar.) >>> >>> I wouldn't think that a C-programmer would have difficulties to confuse >>> semantics of = and op= (especially given that there's many a lot more >>> confusing things in "C" as, e.g., the = and == choice). So "consistency" >>> is as argument vague at best. >> >> "C is so confusing that it doesn't matter if we make it worse" does >> not carry much weight with me. Again - I think it would be fine to >> have an inconsistency between simple and compound assignment, but only >> if there was a very strong reason /for/ the inconsistency. So far, >> you have not given any reason that I can see. > > You probably got me wrong; I wasn't asking for a change of "C". I realise that you are not trying to change C as it is now. (Though if someone felt strongly enough that assignment should return an lvalue, they could write a proposal for C2y. Changing the associativity would be out of the question, but changing the value type would, I think, be feasible.) >> >> Could you give some examples of how you use it in C++? That would, I >> think, make it a lot clearer to me why you think this is a good idea. > > Sorry, I can provide no production code; my professional C++ days were > in the 1990's and I have no access to the code. All I remember was that > I occasionally used such cascades, and that I had to write "spurious" > parenthesis to achieve the left-associativity. Sure. I was not asking for real-life code, merely snippets for demonstration purposes. You have given the one "number *= base += digit" example that I do not find convincing. If you had more, that would carry more weight. >>> >>> So, in C/C++, two separate statements, variable duplication, spurious >>> parenthesis. All adding to the often mentioned "cognitive load". >>> >> >> Multiple statements, duplicated variables and additional parentheses >> can all /reduce/ cognitive load sometimes - though they certainly can >> increase it in other cases. Overly compact code is just as bad as >> overly verbose code. > > Yes, I agree. > >> >> >> If I saw "(number *= base) += digit;" in a code review, I would reject >> it as "smart-arse" coding. > > Well, that's obviously the effect when one is reviewing code based on > one's personal opinion rather than based on a coding standard document > which has rules with rationales associated to every rule. > > (None of the various coding standards I was involved in creating had a > "smart-arse" type of "rationale".) Rules such as "keep expressions simple", "do one thing at a time", "the result of an assignment operator shall not be used" are very common in coding standards. Sometimes they have specific black-or-white rules (such as the MISRA rule "the result of an assignment operator shall not be used"). Sometimes they have more nebulous formulations (like the C++ Core Guidelines rule "Avoid complicated expressions", or the JSF rule limiting the number of side-effects in a single operation). I agree it would be unlikely to see the term "smart-arse" in a formal coding standard :-) > >> My preference would be something like : >> >> const auto scaled_number = unscaled_number * base + digit; > > Different use-case. My code was an excerpt from incrementally creating > the number in a loop. Here's some Algol 68 code for _illustration_ only > (i.e. non-optimized, and using the verbose word-operators instead of > the terse forms, just written just for the post) > > STRING numstr = "12345"; > INT number := 0; > FOR i TO UPB numstr DO > number TIMESAB 10 PLUSAB (ABS numstr[i] - ABS "0") > OD > > Another cascade I was recently writing (with two 'op=' "pipelines") was > > FOR i FROM 1 TO UPB key DO > hash PLUSAB ABS key[i] * pp MODAB hash_modulus; > pp TIMESAB p MODAB hash_modulus > OD > >> >> My strong preference in coding is to initialise variables once, not > > In case of 'op=' you *have* to initialize the variable on the LHS! > Obviously, yes. My preference would be to initialise the variables once, and therefore not use an "op=" expression. Of course sometimes, especially in loops, you do need to change the value of a variable. This is C, not a pure functional language. >> modify them unless it really is helpful (or necessary, such as for >> loops). Unnecessary multiple modifications of one variable in one >> expression? No thanks. >> >> However, that is /my/ preference - other people like to do things >> differently. /My/ personal preferences were not a deciding factor in >> the design of C or C++ ! > > That applies to most people. :-) > > But it's noteworthy, IMO, if you saw discrepancies or infelicities in > a language, and while you can't actually do anything about it, you see > that then changed in some standard two decades later. Or if you see > some time later that the authors of a language are self-critically > speaking about such cases. - All this makes it worthwhile to discuss > the state of things, languages, tools, and standards. - And don't just > accept things as "final wisdom". Agreed. It is also the case that we all use subsets of languages, and it can be interesting to discuss which features we include in our subsets, and which we exclude. Such discussions would be rather boring if we all agreed on our subsets! > > Janis > > PS: I noticed that you repeated things in your post that I had already > been answering. I answered them again in case they were overlooked. > But it makes no sense to continue the subthread that way. > That sometimes happens when writing replies inline, as I read the posts. I've tried to snip a bit more here to reduce any repetitions.
Technical Details
Message-ID:
<11ck72s$2d6d3$1@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-mailReferences:
<11ceoa2$85p3$3@dont-email.me> <11cevfq$ifq3$1@dont-email.me> <11cgp2m$85p3$5@dont-email.me> <11chm17$1hc6k$1@dont-email.me> <11cj6ea$23539$1@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: David Brown <david.brown@hesbynett.no> Newsgroups: comp.lang.c Subject: Re: Rationales for result type of operations using operators like '+=' Date: Fri, 6 Nov 2026 10:36:27 +0100 Organization: A noiseless patient Spider Lines: 358 Message-ID: <11ck72s$2d6d3$1@dont-email.me> References: <11ceoa2$85p3$3@dont-email.me> <11cevfq$ifq3$1@dont-email.me> <11cgp2m$85p3$5@dont-email.me> <11chm17$1hc6k$1@dont-email.me> <11cj6ea$23539$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Fri, 06 Nov 2026 09:36:29 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2529699"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19UH7wLJutGP5851gD8AAZuaFhiLx5xmTw="; posting-host="ab1057a5cce4cf262a2cabef0012b0c0" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:LiM0tKilKCotF0RG8FrH37G218g= sha256:AS0nFAhkTch9weZvYpHbJ23DlZsjCqx1y2PfZ8XrMGw= sha1:WNzCO9JzGIh0I/aGeQRrIQejZ3Q= sha256:ArK/pTidOZsQ8ICxk8qzmxz8Dx/uieLMutfeddUT65g= Content-Language: en-GB In-Reply-To: <11cj6ea$23539$1@dont-email.me>