Today’s post is about expressions. You should avoid complicated expressions, know the precedence rules for arithmetic or logical expressions, and know the order of evaluation of expressions. The main reasons for undefined behavior are having the wrong precedence rules for expressions in mind or assuming an evaluation order for expressions that is just wrong or not guaranteed. I know that’s a lot to digest. Let’s start.
Here are the four rules for today.
- ES.40: Avoid complicated expressions
- ES.41: If in doubt about operator precedence, parenthesize
- ES.43: Avoid expressions with undefined order of evaluation
- ES.44: Don’t depend on order of evaluation of function arguments
The rules for the precedence and the evaluation are not as easy as it sounds. They even change with C++17; therefore, we should start simple.
What does complicated mean? Here is the original example of the guidelines:
I added a few (numbers) to it. First, all expressions having a (1) are bad style and should not pass a code review. For example, do you know what is happening here: x = a & b + c * d && e ^ f == 7;. Of course, you have to look up the precedences of the operators. I will come to it in the following rule. The expression (2) may be acceptable if the conditions hold. i and j must be disjunct, and the indices i,j, and i,k must be pairwise disjunct.
(3) is undefined behavior, because it is not defined which x will be evaluated first. Why? The argument is that the final semicolon “;” is a sequence point, and now we have the guarantee that all side effects from the previous evaluations in the sequence are complete.
With C++17, the rules for operator precedence changed: left-to-right for expressions except for right-to-left in assignments. I will write about it in ES.43.
On the one hand, the guidelines say: If you doubt operator precedence, use parenthesis (1). On the other hand, they state: You should know enough not to need parentheses here (2):
Okay. For an expert, expression (1) may be obvious, but for a beginner, expression (2) may be a challenge.
I have only two tips in mind according to the guidelines:
- If in doubt about precedence, use parentheses. Do not forget the beginners!
- Keep this precedence table from cppreference.com under your pillow.
I will jump right to the rules ES.43 and ES.44 and write about the rule ES.42 in my next post. With C++17, the order of evaluation of expressions changed.
In C++14, the following expression has undefined behavior.
This will not hold for C++17. With C++17, the order of evaluation of the last code snippet is right to left; therefore, the expression has well-defined behavior.
Here are the additional guarantees we have with C++17:
- Postfix expressions are evaluated from left to right. This includes function calls and member selection expressions.
- Assignment expressions are evaluated from right to left. This includes compound assignments.
- Operands to shift operators are evaluated from left to right.
This was the wording of the original proposal. They also provided a few examples. Here are they:
How should you read these examples? Quite simple. First, a will be evaluated, then b, c, and d.
Expression (1) is a bit tricky. With C++17, we can only guarantee that the function is evaluated before its arguments, but the order of the evaluation of the arguments is still unspecified.
I know the last sentence was not easy. Let’s elaborate a little bit more.
In the last years, I saw many errors because developers assumed that the order of the evaluation of function arguments is left to right. Wrong! You have no guarantees!
Here is my proof. The output from Gcc and clang differs:
With C++17, this behavior didn’t change. The order of evaluation is unspecified. But at least the order of evaluating the following expressions is specified with C++17.
Here is the reason why:
(1): Postfix expressions are evaluated from left to right. This includes function calls and member selection expressions.
(2): Operands to shift operators are evaluated from left to right.
(3): Assignment expressions are evaluated from right to left.
Only to remind you. With C++14, the last three expressions have undefined behavior.
Admittedly, this was quite a challenging post but a challenge you must overcome to become a good programmer. The main topic of my next post will be about cast operations.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, moon, and Philipp Lenk.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
|My special thanks to SHAVEDYAKS|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,