# C/C++ const >When it modifies a data declaration, the const keyword specifies that the object or variable isn't modifiable. https://learn.microsoft.com/en-us/cpp/cpp/const-cpp?view=msvc-170 https://learn.microsoft.com/zh-tw/cpp/cpp/const-cpp?view=msvc-170 ## basic ```cpp= //const declar int i = 42; const int j = i; int k = j; const int t; // error unintilized const //const and pointer const int x = 8; int y = 18; const int *xptr1 = &x; //1 point to const int int* const xptr2 = &x; //2 the pointer is const but invalid conversion from ‘const int*’ to ‘int* int const *xptr3 = &x; //3 same as 1 int* const yptr1 = &y; //4 is ok the pointer is const const int* const yptr2 = &y;//5 const point to const int *xptr1 = 10; //is invalid xpt1 = &i; //is ok *yptr1 = 10; //is ok yptr1 = &i; //is invalid //const and reference const int &r1 = i; const int &r2 = 52; int &r3 = x; //error non const reference to const object ``` ## top level and low level const top level: ```cpp= int t1 = 0; int *const pt1 = &t1; const int t2 = 1; ``` low level: ```cpp= const int *pl1 = &t2; const int &rl1 = t2; // const in reference types is always low-level ``` both top and low level: ```cpp= const int *const ptl1 = pt1; //is top level and low level ``` | top level | low level | | ----- | ---- | | value is const | point or ref value is const | | copy can ignore | copy can't ignore | ```cpp= t1 = t2; // is ok pl1 = ptl1;// ok: p2 has the same low-level const qualification as ptl1 int *p = pl1; // error: pl1 has a low-level const but p doesn’t pl1 = &t1; // ok: we can convert int* to const int* pl1 = &t2; int x = *pl1; // ok *pl1 is pointer to top level const t2 int &r1 = t2; // error: can’t bind an ordinary int& to a const int object const int &r2 = t1; // ok: can bind const int& to plain int int y = rl1; //ok rl1 is reference to t2 ``` c++ primer section 2.4.2(P62) ## const and function - const in parmeter ```cpp= void function(int); void function(const int); // redeclares Record function(int) void function(int*); void function(int* const); // redeclares Record function(int*) ``` >top-level const has no effect on the objects that can be passed to the function. A parameter that has a top-level const is indistinguishable from one without a top-level const https://blog.xuite.net/tsai.oktomy/program/65131235# ```cpp= void function(int&); // function that takes a reference to int void function(const int&); // new function that takes a const reference void function(int*); // new function, takes a pointer to int void function(const int*); // new function, takes a pointer to const ``` - const in return type ```cpp= const int& function(const int& i1, const int& i2){ return i1 >= i2 ? i1 : i2; } ``` ```cpp= int function(const int& i1, const int& i2){ return i1 >= i2 ? i1 : i2; } ``` ```cpp= const int function(const int& i1, const int& i2){ return i1 >= i2 ? i1 : i2; } ``` ```cpp= int& function(const int& i1, const int& i2){ return i1 >= i2 ? i1 : i2; //error can't convert const int to int } ``` - const function means function can't modify the data in object https://stackoverflow.com/questions/751681/what-is-the-meaning-of-const-at-the-end-of-a-member-function-declaration ## const type transfer ```cpp= int& function(const int& i1){ return const_casts<int&>(i1); //tranfer const int& to int& } ``` https://en.cppreference.com/w/cpp/language/const_cast https://learn.microsoft.com/en-us/cpp/cpp/const-cast-operator?view=msvc-170 ## const and auto ```cpp= int i = 0, &r = i; auto a = r; // a is an int (r is an alias for i, which has type int const int ci = i, &cr = ci; auto b = ci; // b is an int (top-level const in ci is dropped) auto c = cr; // c is an int (cr is an alias for ci whose const is top-level) auto d = &i; // d is an int* (& of an int object is int*) auto e = &ci; // e is const int* (& of a const object is low-level const) const auto f = ci; // deduced type of ci is int; f has type const int auto &g = ci; // g is a const int& that is bound to ci auto &h = 42; // error: we can’t bind a plain reference to a literal const auto &j = 42; // ok: we can bind a const reference to a literal auto k = ci, &l = i; // k is int; l is int& auto &m = ci, *p = &ci; // m is a const int&; p is a pointer to const int // error: type deduced from i is int; type deduced from &ci is const int auto &n = i, *p2 = &ci; ``` c++ primer 2.5.2 (p69) ## const and pointer ```cpp= //1 int const *h = 5; //2 int *const h = 5; //3 const int *h = 5; //same as 1 //4 const int const *h = 5; //error duplicate const int* const h = 5; //is ok ``` ```cpp= const int z = 2; const int *pz = &z; const int **ppz1 = &pz; int const **ppz2 = &pz; //same as ppz1 int x = 3; int * const px = &x; int * const *ppx = &px; //*ppx value can't be modify and ppx only can point to the pointer which has const value int y = 4; int *py = &y; int ** const ppy = &py; // ppy value can't be change but *ppy value can be change *ppy = &x; //is ok ``` ```mermaid classDiagram pz --|> z ppz --|> pz class z{ value:10 addr()0x0001 } class pz{ value:0x0001 addr()0x0020 } class ppz{ value:0x0020 addr()0x0030 } ``` https://jonny.vip/2018/03/12/cplusplus-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-%E9%A0%82%E5%B1%A4-const-%E9%82%84%E6%98%AF%E5%BA%95%E5%B1%A4-const/ # constexpr >A constant expression is an expression whose value cannot change and that can be evaluated at compile time. c++ primer section 2.4.4(P64) ```cpp= constexpr int mf = 20; // 20 is a constant expression constexpr int limit = mf + 1; // mf + 1 is a constant expression constexpr int sz = size(); // ok only if size is a constexpr functio ``` ### constexpr Functions >A constexpr function is a function that can be used in a constant expression, >A constexpr function is defined like any other function but must meet certain restrictions: The return type and the type of each parameter in a must be a literal type , and the function body must contain exactly one return statement ###### tags: `C++`