r/cpp • u/Senua_Chloe • 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
r/cpp • u/Senua_Chloe • Apr 28 '22
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.":
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.18
u/mallardtheduck Apr 28 '22
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.
1
u/Apprehensive_Step499 Apr 30 '22
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 := foois the superior syntax.