How to use the default keyword in C++ to save time and avoid bugs

Use the =default keyword in your C++ code to save time and avoid bugs! The default keyword tells the compiler to generate default definitions for various trivial class member functions. It's pretty obvious what it does for the constructor and destructor, but have you seen it used for the eg: equality operator on a plain data class? It works here too, and for many other trivial member functions (C++20) It can save a lot of redundant code and avoid nasty bugs down the road when you're adding members to the class - and potentially forget to update the overloaded function. Let the compiler do the heavy lifting! 👇 Example below 👇 #cpp #cplusplus #programming #oop https://lnkd.in/efves8Pr

  • text

This seems very nice as any c++ feature when you first see it. But in fact, this is not reliable. I can understand that it looks great when you add a new field(int, string) and it automatically adds it to the equality function. However, this is not reliable if you add a floating point(float, double). I can share a small example: #include <iostream> struct Person { float salary; bool operator==(const Person& other) const = default; }; int main(int argc, char ** argv) { Person a; a.salary = 0.3; Person b; b.salary = 0.1 * 3; if (a == b) std::cout << "They are equal!" << std::endl; else std::cout << "They are not equal!" << std::endl; return 0; } This prints "They are equal". So far so good, however, if I change to double #include <iostream> struct Person { double salary; bool operator==(const Person& other) const = default; }; int main(int argc, char ** argv) { Person a; a.salary = 0.3; Person b; b.salary = 0.1 * 3; if (a == b) std::cout << "They are equal!" << std::endl; else std::cout << "They are not equal!" << std::endl; return 0; } It prints "They are not equal!"

Stacy Gaudreau Thanks for sharing such a nice utility. As long as each of constituent types has a well defined equality operator this is absolutely fine. For practical purpose, classes with naked pointers or resource handles, the default implementation will not work.

Nice post 👍 =default is definitely a clean and convenient feature, especially for trivial data structures. That said, I’ve seen it backfire a few times in real-world projects when developers used it too casually on non-trivial classes. Once you’ve got pointers, smart pointers, or any resource ownership involved, the compiler-generated equality can silently do the wrong thing - especially with shallow comparisons. Personally, I prefer writing the comparison explicitly when a class manages external resources or has custom semantics. It keeps intent clear and avoids hidden surprises later. So yes, =default is great when used wisely, but like most C++ features, it rewards you only when you truly understand what’s happening underneath.

Honestly, a better example of the compiler saving your fingies is the spaceship operator. Implementing that single operator gives you 6 compiler synthesized gt, lt, gteq, lteq, eq, and neq operator overloads

See more comments

To view or add a comment, sign in

Explore content categories