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.


Deleted functions

In C++11 is it possible to remove a function from a class by using the delete specifier. If you try to use a deleted function will give a compiler error.

As an example, the following class is made non-movable by making it impossible to call the move operator and move constructor:

We can also stop anyone from creating instances of a class on the free store (heap)  by deleting the new operator:

Or more compact by using a template

To stop users from calling a method with specific parameters, delete the functions with the not allowed parameters:



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:



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.