Why should I use nullptr instead of NULL?

C++11 introduced the new keyword nullptr which is used to indicate a null pointer. This is different from the old NULL which was just a zero (usually an integer).

To understand why nullptr was introduced, consider the following example:

the developer probably intended to call the overload of f taking a pointer. However, since NULL is not a pointer but an integer, the overload of f taking an integer is going to be called.

If the developer instead wrote:

then the code would be much more clear in its intent.


Why you should always use a virtual destructor

Virtual destructors are a must when you can delete an object through a pointer to a base class. Let me explain.

Say that you have a simple base class:

and a class deriving from the BaseClass with the addition of a resource which is supposed to be managed using RAII:

now what happens when you use this class:

since the base class doesn’t have a virtual destructor and ‘obj’ is pointing to a BaseClass, not to a DerivedClass, then the destructor of BaseClass is going to be called, but not the destructor of DerivedClass!! This means that the managed resource in DerivedClass is not going to be deleted properly when the object is deleted and thus we have a memory leak!

If instead the BaseClass would have a virtual destructor, like this:

then the constructors would be called in both the base class and the derived class and all contents will be correctly released.


The auto keyword

The auto keyword was changed in C++11 from being completely unnecessary to being a highly useful keyword.

Auto variable type (C++11)

The auto keyword tells the compiler to set the type of a variable to the type of the value that we assign to the variable. For example:

If you don’t assign a value to the variable, then the compiler will be lost and will refuse to compile since it cannot figure out the type to set;

auto can be really useful when some of the standard library operations where the type names can be really complex:

when creating a local lambda expression, then auto is a must:

Auto return type (C++14)

Beginning in C++14 can the return type of a function be auto:

Auto as lambda parameter (C++14)

From C++14 we can also use the auto keyword for the type of the parameters when writing a lambda expression. The lambda itself will then be a generic lambda, i.e. it will be compiled as a template:

will be compiled as:



Range-based for

With C++11 we got a new way of writing for-loops to iterate over all elements in a range. The new range-based for loop is much clearer than the old way using iterators. It is intended to be used when you only look at the elements in a container, you are not supposed to change the container itself since this could cause problems in the loop.

An example of how the new range-based for works, using a vector as an example:

When the vector contains an object then it’s a good idea to get a reference to the object to avoid copying the entire object in each round in the loop.

If the contents of the vector shouldn’t be changed then we can show this by getting a const reference:

We can use the auto keyword to avoid explicitly giving the type:



The constexpr keyword


The keyword constexpr is used to show that a constant is resolved at compile time. This is a new keyword introduced with C++11.

Both constexpr and const are used to show that something is constant and the difference is that a constexpr value must be resolved at compile time  whereas a const can be resolved at run time or at compile time.

In most cases there is no real difference between a compile time or a run time constant but in some cases must the constant be defined at compile time. One example is creating fixed size arrays where the size must be a constant defined at compile time:

constexpr can also be used for function calls

However, a constexpr function must only consist of a single return statement, only call constexpr functions and only access constexpr global/member variables.

The purpose of constexpr is to replace macros and hard coded literal values without sacrificing performance or type safety.


The const keyword

The const keyword is used to show both other developers and the compiler that an object is constant and its contents cannot be changed. If you anyway try to change the contents of a const object will generate an error already at compile time. It is a good practice to add the const keyword to a parameter, a members or a method to show that the value is not going to be changed. This can both make the intent of the code more clear to the user, and can make the code faster since the compiler can make certain optimizations knowing that the object is not going to change its value.

The difference between the const keyword and the constexpr keyword is that the value of a constexpr constant must be resolved at compile time whereas the value of a const constant can be resolved at run time (or at compile time).

The const keyword can be used in the following contexts:

Const class

An object can be made constant at creation by declaring the object as const:

Any attempt to change the contents of the object christmasDay will result in a compiler error:


Const member functions

Adding the const keyword to a member function (added at the end of the function declaration, after the parameters) states that this method will not change the state of the object.

The const keyword after the declaration of the method getYear() shows that calling this method will not change any members of the object. This also means that we are allowed to call this method even if the object is const.


Const parameters

Parameters to functions can also be declared as const. There are two positions where the const keyword can be added.