Mohanad Kaleia

Idea for a cause

Const and constexpr in C++

In C++, there is two type of constant, a constant expression which can be determine at compilation time, and normal const which takes its value during running time, 

const int i = 20; // This is const expression ,

const int j = i; // This is const expression, 

const int k = getSize(); // This constant can't be constant expression because the value of getSize can't be detected else of running time


Note: in C++11 we can define a function to be const expression, but this function should be simple enough so the compiler can get its output during compilation


Mohanad Kaleia, 

C++ Primer

Post a comment

  • *