Const Expression in c++
NOTE: this is just copy-pasting from stackoverflow and some textbooks, see references below.
-
const
: meaning roughly “I promise not to change this value.” This is used primarily to specify interfaces so that data can be passed to functions using pointers and references without fear of it being modified. The compiler enforces the promise made byconst
. The value of aconst
can be calculated at runtime. -
constexpr
: meaning roughly “to be evaluated at compile time.” This is used primarily to specify constants, to allow placement of data in read-only memory (where it is unlikely to be corrupted), and for performance. The value of aconstexpr
must be calculated by the compiler.
You should read from right to left. This is refered to as clockwise/spiral rule
int *
pointer to intint const *
==const int *
pointer to const intint * const
const pointer to intint const * const
==const * const
const pointer to const int
and …
int **
pointer to pointer to intint ** const
const pointer to pointer to intint * const *
pointer to const pointer to intint const **
pointer to pointer to const intint * const * const
const pointer to const pointer to int.
|
|
ref: A tour of C++
https://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-and-int-const
[+] click to leave a comment [+]
the comment system on this blog works via email. The button below will generate a mailto: link based on this page's url and invoke your email client - please edit the comment there! [optional] even better, encrypt the email with my public key - don't modify the subject field - specify a nickname, otherwise your comment will be shown as anonymous - your email address will not be disclosed - you agree that the comment is to be made public. - to take down a comment, send the request via email.>> SEND COMMENT <<