Sometimes, you want to disable the default operations. Here comes delete into the play. C++ eats its own dog food. The copy constructor of types such as locks, mutexes, promises, or futures is set to delete. The same holds true for the smart pointer std::unique_ptr: std::unique_ptr(const std::unique_ptr&) = delete.

You can use delete to create stranges types. Instances of Immortal cannot be destructed.

The C++ standard offers more than 40 specialisations for std::swap. You can use it as a building block for a lot of idioms such as copy construction/assignment. A swap function should not fail; therefore, you have to declare it as noexcept.

Here is an example of a move assignment operation using std::swap. pdata points to an array.

The call MyNumber(5) == 5 is valid because the constructor converts the int argument to an instance of MyNumber. The last line gives an error. The comparison operator for natural numbers will not accept an instance of MyNumber.

The elegant way to solve this asymmetry is to declare a friend operator== inside the class MyNumber.Here is the second version of MyNumber.

Comparing instances of B or instances of D will work. But mixing instances of B and D will not work as expected. Using B's == operator ignores D's character (1). Using D's operator will not work for instances of B (3). The last line is quite tricky. The == operator of B is used. Why? The == operator of D overwrote the == operator of B. Really? No! Both operators have different signatures. One taking an instance of B; the other taking an instance of D. D's version will not overwrite B's version.

This observation will also hold for the other five comparison operators: !=, <, <=, >, and >=.

Hash functions are implicitly used by unordered associative containers such as std::unordered_map. The user doesn't expect that they will throw. If you want to use your own type as a key in an unordered associative container, you have to define a hash function for the key.

Do it by using the std::hash function for the attributes of your class and combine them with ^ (xor).

What's next?

Following the guidelines, the next topic should be containers and other resource handles but only the names of the rules are available. Therefore I will skip this part and go straight to lambda expressions in the next post.

Get your e-book at leanpub:

The C++ Standard Library

Concurrency With Modern C++

Get Both as one Bundle

With C++11 and C++14 we got a lot of new C++ libraries. In addition, the existing ones are greatly improved. The key idea of my book is to give you the necessary information to the current C++ libraries in about 200 pages.

C++11 is the first C++ standard that deals with concurrency. The story goes on with C++17 and will continue with C++20.

I'll give you a detailed insight in the current and the upcoming concurrency in C++. This insight includes the theory and a lot of practice with more the 100 source files.

Get my books "The C++ Standard Library" and "Concurrency with Modern C++" in a bundle.

In sum, you get more than 500 pages full of modern C++ and more than 100 source files presenting concurrency in practice.