I started the last post on my journey through the rules for overloading functions and operators. Let me continue and finish my journey with this post.
First, here are all ten rules for functions and operators.
- C.160: Define operators primarily to mimic conventional usage
- C.161: Use nonmember functions for symmetric operators
- C.162: Overload operations that are roughly equivalent
- C.163: Overload only for operations that are roughly equivalent
- C.164: Avoid conversion operators
- C.165: Use
usingfor customization points
- C.166: Overload unary
&only as part of a system of smart pointers and references
- C.167: Use an operator for an operation with its conventional meaning
- C.168: Define overloaded operators in the namespace of their operands
- C.170: If you feel like overloading a lambda, use a generic lambda
Our journey continues with rule C.164. That is quite an important rule.
If you want fun, overload the operator bool and make it not explicit. This means that type conversion from bool to int can happen.
But I should be serious. Let me design a class MyHouse, which can be bought by a family; therefore, I decided to implement the operator bool to check if a family has bought the house efficiently.
Now, I can quickly check with the operator bool (1) if a family (4) or no family (3) lives in the house. Fine. But due to the implicit operator bool, I can use my house in arithmetic expressions (5). That was not my intention.
This is weird. Since C++11, you can make conversion operators explicit; therefore, no implicit conversion to int will kick in. I have to make the operator bool explicit (2), and adding houses is not possible anymore, but I can use a house in logical expressions.
Now, the compilation of the program fails.
This rule is quite unique; therefore, I will make it short. There are about 50 overloads for std::swap available in the C++ standard. It’s pretty probable that you already implemented swap for your own type: C++ Core Guidelines: Comparison, Swap, and Hash.
Because of argument-dependent lookup (see C.168), the compiler will find your swap implementation. Using the generic std::swap as a kind of fallback is a good idea. std::swap maybe not be optimized for your data type, but at least it works. You can achieve that by introducing the function std::swap.
Honestly, this rule is way too special to write about in this post. If you want to create a proxy by overloading the unary operator &, you should know the consequences.
This rule is similar to rule C.160: Define operators primarily to mimic conventional usage. I referred to it in my last post: C++ Core Guidelines: Rules for Overloading and Overload Operators.
This rule applies to a lot of operators.
- <<, >>: input and output
ADL is a special property in C++, making our life as a programmer easier. ADL stands for argument-dependent lookup. Sometimes it is called Koenig lookup. It means that for unqualified function calls, the functions in the namespace of the function arguments are considered by the C++ runtime. For more details about ADL, read here: argument-dependent lookup.
Only to remind you and give you a short example: because of ADL, the C++ runtime will find the right operator == in the namespace of the operands.
This rule is relatively easy to get. You can not overload a lambda. With C++14, you can overcome this restriction by implementing a generic lambda.
Maybe you know it. A lambda is just an instance of a class for which the call operator is overloaded, or to say in other words, a function object. In addition, a generic lambda is a function object with a templatized call operator. That’s all.
There are four rules for the particular class type union. I’m unsure if I will dedicate the next post to unions. Afterward, I’m done with classes and class hierarchies and will write about enumerations.
I’m pretty happy that I made this post just in time because I had a lot of fascinating discussions about the future of C++ at the Meeting C++ in Berlin.
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,