[c++11] const and constexpr

We all know that const is used when we want to define a variable whose value cannot be changed.

A few points to note when initializing

1. Must be initialized when defining

const int i = 42;//correct
const int k;//wrong,const should be initialized

2. Can be initialized with non-constant

int i = 42;
const int j = i;//Correct, the value of j has nothing to do with i after the copy is completed

3. Only works within the file by default

If multiple files declare const variables with the same name, the compiler considers them to be different variables. If you want to use the same const in multiple files, you need to add the extern keyword when you declare and define it, so you only need to define it once.

Two const references

Although the reference to const is referred to as "constant reference" for short, whether the reference object is constant or non-const only determines the operations it can participate in, but does not affect the binding relationship itself. In fact, the C++ language does not allow arbitrary changes to reference-bound variables, so it can be understood that all references are constant references (the reference relationship is constant).

A reference object can be a non-constant, and a constant reference only limits the operations that the reference can participate in, for example:

int i = 42;
const int &r2 = i;
r2 = 0;//error, i cannot be changed by r2

The so-called constant pointer or reference, but the pointer or reference itself thinks to point to the constant, so "consciously" does not change the value of the object to which it refers.

Three const pointers

Constant pointers must be initialized, and after initialization, the value cannot be changed.

int errNumb=0;
int *const curErr=&errNumb;

Four top and bottom

top-level const: the pointer itself is a constant (cannot change the value)

The underlying const: the object pointed to by the pointer is a constant (the value can be changed)

int i = 0;
int *const p1 = &i;//The value of p1 cannot be changed, it is a top-level const
const int ci =42;//The value of ci cannot be changed, this is also a top-level const
const int *p2 =&ci;//You can change the value of p2, which is an underlying const

A non-constant can be converted to a constant, but not vice versa.

Five constexpr and constant expressions

C++11 stipulates: allow variables to be declared as constexpr types so that compile-time verification of whether the value of the variable is a constant expression

constexpr int sz=size();

In the above example, the sound statement is correct only if size() is a constexpr function.

Constant expression: An expression whose value does not change and the result of the evaluation can be obtained during compilation.

const int sz = function();//sz is not a constant expression, although sz itself is a constant, the fucntion() value cannot be obtained at the compile stage

The difference between const and constexpr is:

1. const does not distinguish between compile-time constants and run-time constants, and constexpr defines compile-time constants.

2. If a pointer is defined in the constexpr declaration, constexpr is only valid for the pointer, not for the object it refers to. That is, constexpr can only be top-level.

const int *p=0;//p is a pointer to a constant
constexpr int *p1 =0;//p1 is a constant pointer to an integer

Six auto and const

auto ignores the top-level const and keeps the bottom-level const.

const int ci=i;
auto b = ci;//b is an integer, the top-level const attribute of ci is ignored
auto e= &ci;//e is a pointer to an integer constant, because taking the address of a constant is an underlying const


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325151434&siteId=291194637
Recommended