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

Introduction to C++11

In August 2011, a new C++ version which is known as C++11 is approved by ISO which adds a lot of new set of features to existing C++ programming language. All official documents related to these changes can be found at ISO C++ committee website.

Why C++11 ?

C++11 significantly improved the core C++ language by making several additions to its standard libraries. Major areas where C++11 is improved from its predecessor includes multithreading support, uniform initialization, generic programming support and performance.

Aims of C++11

  1.  Compatibility and Stability MUST be maintained for old code written in C++98,C++03 and in C.
  2.  Extension of core language preferably be done via standard library.
  3.  System and library design method is preferred compared to introducing new features which is useful only for specific applications.
  4.  Focus to improve type safety by providing safer methods to earlier unsafe methods.
  5.  Improve performance and better interaction with hardware to ensure better,safe and high performance embedded system programming.
  6.  Make C++ easier to teach and learn through increased uniformity, stronger guarantees, and facilities better libraries for beginners. Expert programmers can use other efficient features.
(more…)
Introduction to C++11 Read More