C++ Core Guidelines: More Rules for Overloading


I started in the last post my journey through the rules for overloading of functions and operators. Let me continue and finish my journey with this post.



At first, here are all 10 rules for functions and operators.


Our journey continues with rule C.164. To be honest that is quite an important rule.

C.164: Avoid conversion operators

If you want to have 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 decide to implement the operator bool because I want to easily check if a family already has bought the house.


// implicitConversion.cpp

#include <iostream>
#include <string>

struct MyHouse{
  MyHouse() = default;
  MyHouse(const std::string& fam): family(fam){}
  operator bool(){ return not family.empty(); }               // (1)
  // explicit operator bool(){ return not family.empty(); }   // (2)
  std::string family = "";

int main(){
  std::cout << std::boolalpha << std::endl;
  MyHouse firstHouse;
  if (not firstHouse){                                        // (3)
    std::cout << "firstHouse is already sold." << std::endl;
  MyHouse secondHouse("grimm");                               // (4)
  if (secondHouse){
    std::cout << "Grimm bought secondHouse." << std::endl;
  std::cout << std::endl;
  int myNewHouse = firstHouse + secondHouse;                  // (5)
  auto myNewHouse2 = (20 * firstHouse - 10 * secondHouse) / secondHouse;

  std::cout << "myNewHouse: " << myNewHouse << std::endl;
  std::cout << "myNewHouse2: " << myNewHouse2 << std::endl;
  std::cout << std::endl;


Now, I can easily 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 just to make the operator bool explicit (2) and the addition of houses is not possible anymore, but I can use a house in logical expressions. 

Now, the compilation of the program fails.


C.165: Use using for customization points

This rule is quite special; therefore, I will make it short. There are about 50 overloads for std::swap available in the C++ standard. It's quite probable that you already implemented swap for your own type: C++ Core Guidelines: Comparison, Swap, and Hash.


namespace N {
    My_type X { /* ... */ };
    void swap(X&, X&);   // optimized swap for N::X
    // ...


Because of argument-dependent lookup (see C.168), the compiler will find your implementation of swap. It's a good idea to use the generic std::swap as a kind of fallback. std::swap maybe not optimised for your data type, but at least it works. You can achieve that by introducing the function std::swap.

void f3(N::X& a, N::X& b)
    using std::swap;   // make std::swap available
    swap(a, b);        // calls N::swap if it exists, otherwise std::swap


C.166: Overload unary & only as part of a system of smart pointers and references

To be honest this rule is way too special to write about it in this post. If you want to create a kind of proxy by overloading the unary operator &, you should know the consequences.

C.167: Use an operator for an operation with its conventional meaning

This rule is quite 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
  • ==, !=, <, <=, >, and >=: comparison
  • +, -, *, /, and %: arithmetic
  • ., ->, unary *, and []: access
  • =: assignment

C.168: Define overloaded operators in the namespace of their operands

ADL is a special property in C++ which makes our life as a programmer a lot easier. ADL stands for argument-dependent lookup. Sometimes it is called koenig lookup. It means that for unqualified function calls additionally 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.


namespace N {
    struct S { };
    bool operator==(S, S);   // OK: in the same namespace as S, and even next to S

N::S s;

bool x = (s == s);  // finds N::operator==() by ADL

C.170: If you feel like overloading a lambda, use a generic lambda

This rule is quite easy to get. You can not overload a lambda. With C++14 you can overcome this restriction because you can implement a generic lambda.

auto g = [](int) { /* ... */ };
auto g = [](double) { /* ... */ };   // error: cannot overload lambdas

auto h = [](auto) { /* ... */ };   // OK


Maybe you know it. A lambda is just an instance of a class for which the call operator is overloaded, or to say it with other words a function object. In addition, a generic lambda is a function object with a templatised call operator. That's all.

What's next?

There are four rules for the special class type union. I'm not sure if I will dedicate the whole next post to unions. Afterwards, I'm done with classes and class hierarchies and I will write about enumerations.

I'm quite happy that I made this post just in time because I had a lot of very interesting 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, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Louis St-Amour, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Tobi Heideman, Daniel Hufschläger, Red Trip, Alexander Schwarz, Alessandro Pezzato, Evangelos Denaxas, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Dimitrov Tsvetomir, Leo Goodstadt, Eduardo Velasquez, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Haken Gedek, and Bernd Mühlhaus.


Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, and Rusty Fleming.



My special thanks to Embarcadero CBUIDER STUDIO FINAL ICONS 1024 Small


My special thanks to PVS-Studio PVC Logo



I'm happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.

Bookable (Online)


Standard Seminars (English/German)

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.


Contact Me

Modernes C++,



0 #1 Kristopher 2017-12-27 20:08
Hi all, here every person is sharing these
know-how, thus it's pleasant to read this website, and I used
to visit this weblog all the time.

My Newest E-Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Interactive Course: The All-in-One Guide to C++20

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code


Today 33

Yesterday 7842

Week 7875

Month 41253

All 8160399

Currently are 242 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments