C++11: Override and Final keyword

The explicit override and final keywords in C++11 (Introduction to C++11) enhances the polymorphism capabilities of C++. These new features provide extra control over virtual functions to the programmer. In this post, we will discuss about these new features and their usage.

Override

The explicit override keyword overrides the marked virtual function in derived class as override version of base class virtual functions. This ensures that the overrides are done by the programmer intentionally which reduces the chances of unwanted mistakes by providing clarity and hence improves the code readability and maintainability.

(more…)
C++11: Override and Final keyword Read More

C++11: Uniform Initialization explained with Simple example

In C++11 (Introduction to C++11), “Uniform initialization” syntax is introduced to initialize variables which provides a consistent syntax for all types of data variables initialization. This syntax is defined for all types of objects, arrays etc. In this post we are focusing on Uniform initialization syntax with some coding examples.

Earlier there were multiple ways to initialize variables. For example: basic data type variables could be initialized using assignment operator whereas class objects could be initialized using a constructor.

In C++11, a uniform way of initializing variables is introduced which is called “Uniform Initialization” syntax.

Let’s have a look at the sample code example to understand the usage of uniform initialization.

(more…)
C++11: Uniform Initialization explained with Simple example Read More

C++11: Initializer Lists Explained with Simple Example

In C++11 (Introduction to C++11), Initializer lists are a major addition to the C++ language. In this post we are focusing on Initializer lists with some coding examples.

To understand Initializer lists, we need to first understand the constructors. Constructors are special member functions of a class which is used to initialize the object in desired way. Constructors has the same name as that of class. There could be multiple constructors defined in a class which could be selected based on invoking conditions.

(more…)
C++11: Initializer Lists Explained with Simple Example Read More

C++11: extern template Explained With Simple Example

In C++11 (Introduction to C++11), extern template is added to optimize the compile time and object size. Earlier C++ compiler (C++ 03) instantiate a template function or class whenever it encounters a fully defined template function or class. If the template is used in multiple files, then compiler must create multiple instances of template function or class only to later discard all the instance but one. This would result in extra compile time and increased object file size. In C++ 03 there was no way to avoid this.

In C++11, extern template feature is introduced to avoid this situation.
Format of extern template is as follows

extern template void DemoTemplateFunction <int> ();
extern template class NUM <type>;
(more…)
C++11: extern template Explained With Simple Example Read More

Difference between constexpr vs inline functions

constexpr functions are implicitly inline functions only. However, inline functions are simply removes the overhead of function calls by pasting the function body at the called place. But, these code will be executed at run time. Hence, performance gain will be because of reduced function calls.

constexpr functions are evaluated at the compile time to ensure no computation at runtime and hence gives better performance optimization.

Any member functions can be made inline. However, only those functions whose return types and argument types are all of literal types.

For more information related to constexpr click here.

Difference between constexpr vs inline functions Read More

Difference between constexpr vs const

constexprconst
If a member function is declared as constexpr then it will be treated as const as well.If a member function is declared as const then it will not be treated as constexpr.
If a variable is declared as constexpr then it will be treated as const as well.If a variable is declared as const then it will not be treated as constexpr.
constexpr is mainly used for optimization purpose where computation can be done at compile level to optimize run time performance.However const is mainly used to ensure that there are no accidental changes done by the member method to member data.

For more information related to constexpr click here.

Difference between constexpr vs const Read More

constexpr explained with simple example

In C++11(Introduction to C++11), constexpr is added which improves the run-time performance of the code as it allows the compiler to compute the associated code at compile time if possible. constexpr specifier can be used with both variables and functions where only constant expressions are used which can be evaluated at compile time.

Few important points:

  1. A constexpr function return type and argument type must be literal types.
  2. A constexpr function cannot be virtual but it can be recursive.
  3. A constexpr function cannot have uninitialized variables. Till C++11 it can have only one return statement while from C++14 it can have more than one return statement.
  4. Any variable can be declared as constexpr, if it is initialized. In case of constructor initialization, the constructor also must be declared as constexpr.
  5. constexpr can also be used with references if the referenced object is initialized.
  6. Any constexpr function can only call other constexpr functions.
  7. constexpr function can also be used with non-const variables. No need to define two different functions for same.

(more…)

constexpr explained with simple example Read More

Move Assignment Operator explained with simple example

In C++11 (Introduction to C++11) , move assignment operator are added which can improve any code’s performance drastically. Typically a move assignment operator is same as copy/move constructor but before reusing the memory blocks from passed object, it releases all the memory which is owned by this object. Since, memory reallocation is avoided in case of move assignment operators. Thus it gives better performance.

However while implementing move assignment operators we need to take care of one important point to ensure that the original object can be correctly destroyed.

Let’s take an example to see how this memory reallocation is avoided in case of move assignment.

(more…)

Move Assignment Operator explained with simple example Read More

std::move explained with simple example

std::move is a C++11(Introduction to C++11) utility function which is used to convert an lvalue to rvalue expression to forcefully call the move constructor or move assignment operator which will improve the program’s performance drastically. This utility function is only change lvalue into rvalue expression that’s it. This function doesn’t do any kind of memory movement.

Implementation of this function is as follows:

From C++11 onwards:

template< class T >
typename std::remove_reference<T>::type&& move( T&& t );

From C++14 onwards:

template< class T >
constexpr typename std::remove_reference<T>::type&& move( T&& t );

Let’s take an example of usage of this move function:

(more…)
std::move explained with simple example Read More

Move Constructors explained with simple example


In C++11 (Introduction to C++11) , move constructors are added which can improve any code’s performance drastically. Typically a move constructor is same as copy constructor which will create a new instance based on the passed object. But, the idea behind move constructor is to avoid memory reallocation and use as many memory from the passed original object because the original object is about to be deleted as it has been provided as a temporary object.

Since, in move constructors we avoid memory reallocation while creating new instance while will improve the code’s performance as we know memory allocation is quite costly.
While implementing move constructors we need to take care of one important point to ensure that the original object can be correctly destroyed.

Let’s take an example to see how this memory reallocation is avoided in case of move constructor.

(more…)

Move Constructors explained with simple example Read More