[SUGGESTION] eliminate operator precedence (and require parens) #623
Replies: 11 comments 9 replies
-
I recall that Rust, They divide them into arithmetic ones, bit-wise ones, and so on. We can have a similar classification, |
Beta Was this translation helpful? Give feedback.
-
Carbon uses some kind of partial precedence to determine order of operators. Here. |
Beta Was this translation helpful? Give feedback.
-
Here's an example by Sean Baxter from "Circle Evolves C++ | Sean Baxter | NYC++": https://youtu.be/P1ZDOGDMNLM?t=901. Line 1623 in ecd3726 See also https://youtu.be/P1ZDOGDMNLM?t=1926. |
Beta Was this translation helpful? Give feedback.
-
IIUC "Circle Evolves C++" uses Carbon's precedences (after a In comparison, "mandate parentheses when combining multiple binary operators" is a single, short sentence. To me, this seems |
Beta Was this translation helpful? Give feedback.
-
Another point against "just re-organize the precedence table" is that a .cpp2 file can mix C++1 and C++2 code. It'd be weird if, for a line of code like In contrast, with mandatory parens:
It doesn't even have to be copy/paste. cpp2 aims to support gradual migration. You can start with your |
Beta Was this translation helpful? Give feedback.
-
I think I agree with you here. Taking the conservative approach also allows for changes in the future becaude any code with use of parens will never become illegal. On the other hand, if we determine the precedence now and realise that it needs changes, existing code will have to be modified. |
Beta Was this translation helpful? Give feedback.
-
This is true in a vacuum. But in our actual culture you would have to explain to everyone why About the other operators I agree. They're made up anyway, so no need to make up precedence rules that no one bothers memorizing because they're made up. |
Beta Was this translation helpful? Give feedback.
-
Seeing as shifts are merely multiplication/division by power of 2, there's a mathematical argument that C++'s precedence is more correct than Go's. D also does this.
D's reference compiler requires parentheses for any equality/relational operation where one side is an
More generally I suggest making implicit bool to integer conversion not work in an expression, to prevent bugs. Edit: Fixed last line of conditional. |
Beta Was this translation helpful? Give feedback.
-
I personally always used parentheses for the bitwise operators, because
The bad operator precedence comes from the early K&R days and it should have been fixed then, but wasn't. As others mentioned before fixing it in cppfront is not really an option because cpp2/cppfront and C++ will coexist. Partial precedence ordering (used by for example carbon) seems like a fine solution. Force the coder to disambiguate. |
Beta Was this translation helpful? Give feedback.
-
Thanks! I think I've answered this, and addressed it for Briefly: It's reasonable for Operator overloading is fine, but I view |
Beta Was this translation helpful? Give feedback.
-
I have an use case for abusing |
Beta Was this translation helpful? Give feedback.
-
Is
3 << 5 - 1
parsed as(3 << 5) - 1
or3 << (5 - 1)
? C++ and Go give different answers!Similarly,
6 & 2 == 2
is6 & (2 == 2)
in C++ but(6 & 2) == 2
in Go. These reduce to6 & 1
for C++ and2 == 2
for Go, which are obviously different (0
andtrue
).For reference: C++ Operator Precedence and Go Operator Precedence
Like any curly-bracket language, Go looks a lot like C++, but copy/pasting a Go snippet from stackoverflow.com into a C++ program might have subtly different semantics.
I acknowledge that the SUGGESTION template says:
However, it also says "Please limit suggestions to quantifiable improvements to C++ simplicity, safety, or toolability" and there is a safety angle here. Operator precedence ambiguity has been a source of bugs in parsers. For example, in a library that emphasizes safety: jbangert/nail#7
Simplifying (by eliminating the idea of and need for precedence) is also similar to simplifying C++'s
const T*
type syntax. In C++, is it(const T)*
orconst (T*)
? In cppfront,* const T
is unambiguous, reading left-to-right.The concrete suggestion: mandate parentheses when combining multiple binary operators. The programmer has to explicitly write either
(3 << 5) - 1
or3 << (5 - 1)
. A bare3 << 5 - 1
would be a compiler error.Beta Was this translation helpful? Give feedback.
All reactions