C++ Techniques / Idioms

C++ Techniques / Idioms

1. Resource acquisition is initialization (RAII)


Binds the life cycle of a resource that must be acquired before use (allocated heap memory, thread of execution, open socket, open file, locked mutex, disk space, database connection—anything that exists in limited supply) to the lifetime of an object.


  • Wraps over a resource
  • Release the resource when the lifetime of the object ends (even through an exception which causes stack unwinding)
  • Instances of the class are stack allocated

2. Rule of 0/3/5 – The rule of  “All or nothing”

If a class defines one of the Big Five, it should probably define all of them.

  • copy constructor
  • copy assignment operator
  • destructor
  • move constructor
  • move assignment operator

These functions are usually required only when a class is manually managing a dynamically allocated resource, and so all of them must be implemented to manage the resource safely.

3. Private Implementation (PImpl)


Remove compilation dependencies on internal class implementations and improve compile times.
When a header file changes, any files that #include that file will need to be recompiled.
The pimpl idiom allows you to keep binary compatibility of your API between versions. It is meant to help you release a new version of your library without requiring authors of software depending on it to recompile their applications.

As a performance drawback, using pimpl will cause the pointer dereference for all your function calls.

4. Curiously recurring template pattern (CRTP)


A class X derives from a class template instantiation using X itself as template argument.


5.1 Return value optimization (RVO)

RVO is a compiler optimization technique that allows the compiler to construct the return value of a function at the call site.

5.2 Short string optimization (SSO)

SSO implies using the stack to allocate a small-enough string instead of using the heap to do it.

– we are on a 64-bit machine
– size of pointer is that of 8 bytes
– we have a string of <= 16 characters (null-terminating char included)

The string will have the size of two pointers.
The std::string implementation requires three pointers.
The text is small enough to fit into the stack-allocated part.

5.3 Empty base optimization (EBO)

The size of any object or member subobject is required to be at least 1 even if the type is an empty class type (that is, a class or struct that has no non-static data members), in order to be able to guarantee that the addresses of distinct objects of the same type are always distinct.

6. The hidden ‘this’ pointer

Compiler vs class functions

When the compiler finds a class member function, it implicitly adds a new parameter to the function named “this”. The this pointer is a hidden const pointer that holds the address of the object the member function was called on.

7. Object slicing


Object slicing happens when a derived class object is assigned to a base class object, additional attributes of a derived class object are sliced off to form the base class object.
Object slicing doesn’t occur when pointers or references to objects are passed as function arguments since a pointer or reference of any type takes same amount of memory.

For the following code

we can notice the following:

  • We have two derived objects
  • base will now reference obj1
  • that same memory address will be re-assigned to obj2

After the last two points, obj1 will have the following:

  • Base data from obj2 (the value of _i)
  • Derived data from obj1 (the value of _j)

You may also like...

Leave a Reply