Const qualifiers, constexpr, and constant expressions------C + + Primer

Source: Internet
Author: User

The compiler will replace the const variable with the corresponding value during the compilation process, in order to perform this substitution, the compiler must know the initial value of the variable. If the program contains multiple files, then the file with the Const object must have access to its initial value. To do this, you have to define it in every file that uses the variable. To support this usage while avoiding duplicate definitions of the same variable, the const object is set to be valid only within the file by default. When a const variable of the same name appears in multiple files, it is actually equivalent to defining separate variables in different files.

Sometimes there is a const variable whose initial value is not a constant expression, but it does need to be shared between files. In this case, we do not want the compiler to generate separate variables for each file separately. Instead, we want such const objects to work like other (very cons) objects, meaning that they are defined in only one file and are declared and used in several other files.

The workaround is to add the extern keyword for the const variable, whether it is a declaration or a definition, so that it can be defined only once:

file_1.cc defines and initializes a constant that can be accessed by other files by the extern const int bufsize = FCN ();//File_1.h header file extern const int bufsize;//and file_1. The bufsize defined in CC is the same

Because BufSize is a constant, it must be qualified with extern so that it is used by other files.

The declaration in the File_1.h header file is also defined by extern and its role is to indicate that bufsize is not unique to the present document and that its definition will appear elsewhere.

If you want to share a const object between multiple files, you must add the extern keyword before the definition of the variable.

const double *CPTR; pointer to constant. It cannot be used to change the value of the object it refers to, it can point to a very important object, and the so-called pointer to a constant simply requires that the value of the object cannot be changed by the pointer, without specifying that the value of that object cannot be changed by other means.

a double const *cptr; A constant pointer, that is, the value of the pointer itself rather than the value pointed to by the pointer.

Using the top-level const means that the pointer itself is a constant, with the underlying const representing the object that the pointer refers to is a constant.

const int *const p3=p2;//on the right is the top-level const, and the left is the underlying const

  

Constexpr and constant expressions

Constant expressions (const expression) are expressions that do not change the value and can be evaluated during compilation . Whether an object (or an expression) is a constant expression is determined by its data type and initial value, for example:

const int Max=20;//max is a constant expression const int li=max+1;//li is a constant expression int sta=27;//sta is not a constant expression const int sz=get ();//sz is not a constant expression

Although SZ itself is a constant, its specific value knows that the runtime can get it, so it is not a constant expression.

CONSTEXPR variable

The constexpr type will be compiled by the compiler to verify that the value of the variable is a constant expression. A variable declared as constexpr must be a constant and be initialized with a constant expression:

constexpr int MF=20;//20 is a constant expression constexpr int li=mf+1;//mf+1 is a constant expression constexpr int sz=size ();// The correct declaration statement is only when size is a constexpr function

If the determined variable is a constant expression, declare it as a constexpr type, and only the literal type can be defined as constexpr.

In the data types that have been touched so far, arithmetic types, references, and pointers are literal types, IO libraries, and string types are not literal types and cannot be defined as constexpr

Although pointers and references can be defined as constexpr, their initial values are strictly limited, and the initial value of a constexpr pointer must be nullptr or 0, or an object stored in a fixed address.

The constexpr pointer cannot point to a variable defined in the function body (because it is generally not stored in a fixed address).

Objects that are defined outside of all function bodies have fixed addresses that can be used to initialize the CONSTECPR pointer.

Pointers and constexpr

If a pointer is defined in the CONSTEXPR declaration, the qualifier is valid only for pointers, regardless of the object to which the pointer refers;

The constexpr pointer can either point to a constant or point to a very good amount.

Const qualifiers, constexpr, and constant expressions------C + + Primer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.