Talk:cpp/language/operator precedence
I just changed the operator precedence for ?: to reflect the fact that it's the same precedence as assignment. To see that this is actually true, use a compiler of your choice, and evaluate the following piece of code:
int a = 0, b = 0; true ? a : b = 7;
After that, both a and b are zero, because due to right-to-left associativity (and equal precedence), the compiler sees it as (true ? a : (b = 7)), and will simply evaluate the a branch. If ?: was actually higher precedence than =, the value of a should be 7. Add parentheses to verify. Yes. It is surprising that essentially all precedence tables for C/C++/Java are wrong.
24.5.77.206 15:19, 10 September 2012 (PDT)
- Indeed, the grammar reference of the C++ standard lists the assignment and conditional operators as having the same precedence. Thanks for noticing. -- P12 15:41, 10 September 2012 (PDT)
- Good exposition of this corner of the grammar: http://stackoverflow.com/a/13515505/273767 --Cubbi (talk) 14:30, 27 December 2013 (PST)
Contents |
[edit] A simpler version
A link to http://cpp.operator-precedence.com was added to this page, presumably with the idea that it would be useful to have a simpler view of this chart. I've tentatively reverted the change, just to make sure it's not linkbait. Is such a link useful? Alternatively, could this chart be made clearer or simpler? One possibility might be to use color to help convey information. -Nate 00:45, 3 November 2012 (PDT)
- The current table gives more information and is more readable than the one at operator-precedence.com.
- The colors they chose to categorize the operators are hardly distinguishable, using brighter colors would make the page look very messy.
- --Bazzy 04:22, 3 November 2012 (PDT)
- I have the same opinion. P12 05:05, 3 November 2012 (PDT)
[edit] Precedence of throw
Is there a reason for throw to have lower precedence than assignment and conditional? Looking at the grammar, this doesn't seem to be the case. Also, if this were true, false ? 7 : throw 3; should be parsed as (false ? 7 : throw) 3; (throw by itself is a valid throw-expression), which would be a syntax error. However, all compilers that I tested parse it as false ? 7 : (throw 3);, which is consistent with conditional and throw having the same precedence and grouping right-to-left. --bogdan (talk) 11:23, 9 May 2015 (PDT)
- interesting point.. you might be right. The grammar (A.4[gram.expr]) says
assignment-expression: conditional-expression logical-or-expression assignment-operator initializer-clause throw-expression
meaning one level above comma are all three of ?:, compound assignments, and throw. It's been that way all the way back to 1998. Good job finding a bug in probably the most visited page! --Cubbi (talk) 13:14, 9 May 2015 (PDT)
[edit] new[] syntax
I'm back, nitpicking on the best C++ operator precedence table on the Web. The third group lists new[] as an operator, but, as far as I know, this syntax cannot appear as an operator in an expression. It does appear in the name of an allocation function, but I don't think that's relevant to operator precedence. bogdan (talk) 04:39, 31 October 2015 (PDT)
[edit] Suggestion: Adding a newbie friendly footnote to << >> operators
Hi, as a programming newbie, I was at first confused that the bitwise left and right shift operators were not called the stream operators, as these operators are often first encountered as such in most C++ tutorials. After reading the explicatory footnotes, I now understand that overloading does not change precedence and the confusion is cleared. I would nonetheless suggest making a footnote link at the << >> operators, explaining that these operators are frequently first encountered in overloaded form.
- expanded the note on overloading with std::cout << as an example - is that what you're thinking of? --Cubbi (talk) 08:26, 12 October 2016 (PDT)
[edit] Assigning numbers to precedence levels
(Regarding the latest revision.) Should higher precedence operators be assigned bigger or smaller numbers? And should there be any assignment at all for that matter? Reviewing Google's results for "operator precedence" gives the following.
The following sources number precedences from higher to lower:
- https://en.wikipedia.org/wiki/Order_of_operations
- https://dev.mysql.com/doc/refman/8.0/en/operator-precedence.html (implicitly through line numbers)
- https://docs.microsoft.com/en-us/sql/t-sql/language-elements/operator-precedence-transact-sql
- https://www.mathworks.com/help/matlab/matlab_prog/operator-precedence.html
The following sources number precedences from lower to higher:
The following sources do not number precedences:
- https://www.php.net/manual/en/language.operators.precedence.php
- https://docs.python.org/3/reference/expressions.html
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
- https://mariadb.com/kb/en/operator-precedence/
- https://docs.microsoft.com/en-us/cpp/c-language/precedence-and-order-of-evaluation
- https://developer.apple.com/documentation/swift/swift_standard_library/operator_declarations
- https://www.maplesoft.com/support/help/Maple/view.aspx?path=operators/precedence
- https://www.gnu.org/software/gawk/manual/html_node/Precedence.html
- https://www.lua.org/pil/3.5.html
I feel like numbering from higher to lower is more natural than the reverse.
— Radix (talk) 06:01, 1 February 2021 (PST)
- Thanks for the footwork. I flipped it back. Ultimately the numbers are only meaningful if we refer to them in the text, and we don't: here the text says "a row further below" instead of "a row with the larger number", so we could drop them entirely. However, plenty of online discussions link here and use the numbers in this table when talking about it - changing it to the opposite would invalidate such links unnecessarily. --Cubbi (talk) 10:46, 2 February 2021 (PST)
- PS, a more relevant big wiki page would be https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence - we could borrow from that the "1 (highest)" convention. --Cubbi (talk) 10:49, 2 February 2021 (PST)
[edit] Should pack indexing be listed here?
C++26 pack indexing grammar is part of id-expression just like ::
. While ::
is listed in operator precedence table, I proposed that a...[]
should be here too (with 1st precedence).
--Guyutongxue (talk) 22:15, 7 January 2024 (PST)