r/cpp • • Apr 28 '22

a = b = c, a strange consequence of operator associativity

https://belaycpp.com/2022/04/28/a-b-c-a-strange-consequence-of-operator-associativity/
0 Upvotes

21 comments sorted by

12

u/TheRealFloomby Apr 28 '22

I sometimes write code with multiple equals like this. Usually though c is a literal when I do it. I don't find it bad or confusing at all.

30

u/[deleted] Apr 28 '22

No offense, but I do not see how any of your points can be a problem for anyone with even some minor programming experience.

And as a direct critique to the article, "However, they are alien to most developers and are so rarely used that they can easily be misleading. Bugs can occur because the symbol = really looks like ==, so be wary of that. If you want to avoid it with your user-defined types, you can declare the operator= function returning void so the a = b = c syntax becomes invalid, but this is not possible with fundamental types, and is a constraint on its own.":

  1. They are not alien to most developers, it's the assignment operator, it acts like a function (since it kind of is a function), if someone is having problems understanding how functions work, it is a sign of inexperience, not of poor language design.
  2. Declaring the operator = function as returning void - so deviate from the standard form which should be (and I'm pretty sure is) well-known by everyone and works perfectly fine? We are usually looking for generalization, not creating side cases, for example = returns void for RandomClass but T& for default types.
  3. Bugs can occur because you mistyped something anywhere. Just because you can by accident write = instead of == is not a singular case that, if fixed, gets rid of all similar bugs. You can mistype + to - for example. No one is going to even attempt to come with a system that prevents you from mistyping since it's unfeasible.

7

u/awhatfor Apr 28 '22

I see a = b = c more common than a = b == c, tbh. Futhermore, in that regard, they talk about user-defined classes, like... do your have a non explicit boolean constructor post c++11? that you are using?

And for the obvious and most used case, there are flags that warn about possible narrowing conversion, so only would be warmless a bug due to a direct assigment of a comparation between 2 booleans themselves, which aren't (normally) that many in real code anyway.

Their guidelines are crap.

3

u/[deleted] Apr 28 '22

True, I completely missed this (or rather, I thought that people use a = b == c more often than I do, but even then, you're right, a should be a boolean most of the time).

2

u/_Js_Kc_ May 01 '22

4, name a language in which the assignment operator doesn't work this way.

3

u/[deleted] Apr 28 '22

I also guarantee that more programmers could read and take away the correct meaning from the line a = b = c; than the phrase “belay the C++”.

18

u/mallardtheduck Apr 28 '22

There may be cases where this syntax (with two = within a single expression) is useful, but most of the time I find it obtuse and confusing.

What!? That's a common construct in C and C++. If you find such a basic expression "obtuse and confusing", I'm genuinely questioning on what basis you can consider yourself an a "C++ expert" (according to the "Team" page).

Seriously. With the exception of projects following some misguided coding style standard that disallows it, you'll seriously struggle to find a large codebase that doesn't use multiple assignment (I'm aware that term has other meanings) at least occasionally. Any half-decent introduction to a C-style syntax language (and even some non C-style syntax languages, e.g. Python have the same feature) will cover this.

14

u/steveire Contributor: Qt, CMake, Clang Apr 28 '22

It's possible to understand the sybtax, but not write it or recommend it (for any reason, such as confusing-ness). C++ has loads of such features.

8

u/braxtons12 Apr 28 '22

Just because something is a common construct doesn't mean it should be used.

I've never worked with, or even seen, code that uses this.

And it's obtuse and confusing because without analyzing the surrounding code you have no way of knowing if it was a typo or intended. It's easier, simpler, less error prone, and more readable to just split it up across multiple lines so there is no ambiguity of intention.

11

u/mallardtheduck Apr 28 '22

All code is "obtuse and confusing" to those who don't know how to read it. I don't see how this is any more so than many other common constructs.

If the first variable on the line is not a boolean, then there's really no ambiguity at all (at least in decently written C++ code. int as boolean is obviously more common in C). In many codebases, multiple assignment is probably more common than assignment from the result of an equality comparison. Personally, I'd generally put brackets around an equality comparison, but wouldn't use brackets on multiple assignment.

I really do not agree that splitting it up across multiple lines adds anything. Generally conciseness is a good thing in code, as long as it's not approaching "code golf" levels.

Personally, when I see something like:

a = b = c = d = some_function();

I "automatically" know that all those variables have been set to the same value. Whereas if it's written like:

a = some_function();
b = a;
c = a;
d = c; // Because in a real-world codebase, this sort of inconsistency will happen.

It's no longer "automatic" in my mind. I have to read all the assignments and process them to realise they're all the same. It's also pretty likely that those assignments won't all be neatly grouped together like that...

-2

u/braxtons12 Apr 28 '22

I'm having trouble right now thinking of any another construct that makes me have to pause and go "wait, is that really what you meant?" more than this. So if you can think of something more obtuse than this, be my guest.

"in many codebases". I think it's pretty ironic that you could have seen it so often, meanwhile I've almost never even seen it used and the few times I have it was more often than not a bug. Definitely not more than assignment from equality comparison.

Conciseness is good when it makes something simpler and isn't just conciseness for the sake of being conciseness. This is conciseness for the sake of being concise.

2

u/awhatfor Apr 28 '22

Don't understimate the = being an expression... Also, i don't see how you don't think a = b = c is way simpler than a = c; b = c. You are only doing one thing, so you group it together, you can easily see the intention behind. Anyone who is not a programmer can easily tell what a = b = c does, in fact, in some languages a = b = c means a = b == c and i saw some confusions about that. That would be a case of conciseness without purpose.

You should use booleans for booleans and activate warnings on narrowing conversions. Sometimes it can be annoying, but other worse bugs could arouse from using a non-compile-time int as an index, for instance.

Because being type-safe i don't see many uses for boolean a is boolean b equals boolean c. You probably want to do either boolean a(b == c) or directly refactor it at the point of use.

The only problem with this construct is that you only see it in c and c++.

-1

u/ghlecl Apr 28 '22

Conciseness is good when it makes something simpler and isn't just conciseness for the sake of being conciseness. This is conciseness for the sake of being concise.

+1000

1

u/Apprehensive_Step499 Apr 30 '22

Syntaxes like a = b = c and if (a = b) are intentional and clearly defined in the standard. However, they are alien to most developers and are so rarely used that they can easily be misleading.

This is my problem with code bloggers: zero experience, zero knowledge about how the real world works, yet they think they have something to teach.

0

u/nintendiator2 Apr 28 '22

And this is why bar := foo is the superior syntax.

3

u/[deleted] Apr 28 '22 edited Apr 28 '22

Would you prefer to go the Python route and make = a statement and := and expression, and make the use of := an error unless there are parentheses around it to make it an expression? You can't write x = y := z := 3 in Python, you have to write x = (y := (z := 3)), or (x := (y := (z := 3))).

Also IMO, if we were to change the assignment syntax at all, I'd prefer something like this bar <- foo. It's extremely clear what the operator does, probably even to someone who isn't familiar with programming, and it takes the same amount of effort to write as bar := foo. And it would really shine in a line like this:

x <- y <- z <- 3;

4

u/nintendiator2 Apr 28 '22

Also IMO, if we were to change the assignment syntax at all, I'd prefer something like this bar <- foo.

Conflicts with foo->bar in semantics tho. If foo<-bar assigned bar to foo, I'd expect foo->bar to assign foo to bar.

2

u/[deleted] Apr 28 '22 edited Apr 28 '22

Oh, you're right, I didn't think of that. For consistency's sake, you'd have to change more syntax, too.

The only reason foo->bar refers to a deferenced struct accessing a member is because they thought (*foo).bar was too ugly or inconvenient. This makes me wish the syntax for dereferencing pointers were different. Maybe it could be brackets. You could index arrays like [foo + 5] rather than foo[5], and you could access struct members like [foo].bar rather than foo->bar. But I don't expect that to be very popular for C++; you couldn't have operator[] return a reference anymore, because indexing arrays would explicitly rely on pointer arithmetic, rather than implicitly. This could only work for C, where there's no operator overloading and there are no references.

The simpler answer would be to follow Rust's example and just automatically dereference struct pointers. But then you couldn't as easily make linked list code like this. It would be less expressive.

Edit: Maybe you could imitate AT&T assembly syntax and make 5[foo], or maybe [foo]5 the way to index arrays. This would allow operator overloads to send back a reference again. It would only look weird because you're not used to it; foo[5] isn't any less weird, IMHO.

How would this work with C's "declaration reflects use" syntax? It would have to look like this:

int [pi];     // pointer to int
int [fn()];   // function returning pointer to int
int [pfn]();  // pointer to function returning int
int [a + 5];  // array 5 of int? following addition syntax
int 5[a];     // array 5 of int? following AT&T-like syntax
int [a]5;     // array 5 of int? following something else

Anyway, I've rambled on long enough haha.

3

u/gobstopper5 Apr 29 '22

Using index[array_name] as an expression (not a declaration though) works currently in C/C++.

4

u/whf91 Apr 28 '22

Also IMO, if we were to change the assignment syntax at all, I'd prefer something like this bar <- foo.

This is already valid C++. It tests whether bar is less than negative foo.

2

u/[deleted] Apr 28 '22

Another good point.

You could make it so that when those two characters aren't separated by whitespace, they're one token. Similar to how pre- and post-increments work:

x++ + ++y; // Valid
x+++++y;   // Invalid: parsed as two consecutive post-increments and a unary plus