This is 4th blog post about introduction to c++ 11 standard with examples. In this post we will study about c++ 11 default, delete, and constexpr keyword.

introduction to c++ 11 standard with examples (tutorial 4)introduction to c++ 11 standard with examples (tutorial 4)

Keyword default:

Let us see below code and try to compile: g++ -std=c++11 main.cpp -o main

The program will give compile time error:

why does compilation fail?

As we know that if a class is having any constructor defined by user then compiler does not generate default constructor. so in the above program for class foo we have defined a constructor foo(int b) so the default constructor foo() {} would not be generated by compiler.

Force compiler to generate default constructor by default keyword.

Keyword delete: deleting a function

Let us suppose we have a very simple class as follows:

We have defined foo class constructor which is taking int value but created foo obj by passing float value. The compiler does a conversion for you during compilation. It converts float to int. This type of conversion is known as implicit conversion.

This type of implicit conversion can be prevented by declaration of deleted function using delete keyword. In the below code the declaration foo(float b) = delete; will prevent making object of class foo by passing float value.

constexpr in c++ 11

Read next link for constexpr in c++ 11