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