Literal and literal types of C + +

Source: Internet
Author: User

A value of a shape such as 42 is called a literal constant. The form and value of a literal constant determines the type of the constant. For example, 0x42 is an integer constant of 16 binary representations. ' A ' is a char literal. Literal literals are constants, as the name implies, literally. Literal constants are directly represented in the program, and integers are written directly out of size, and characters are written out directly. A literal constant is parsed directly to the immediate number at compile time, and the type of the literal constant is maintained internally by the compiler.

A constant expression is an expression that does not change the value of the expression during compilation, and that results can be obtained immediately during compilation. A part of a const object is a constant expression, and a const object initialized by a constant expression is also a constant expression. Constant expressions do not change while the program is running, and the value does not change even if a program is started more than once or the external parameters change. At compile time, the compiler may replace the constant expression directly with the immediate number, depending on the compilation environment. In general, literal constants belong to constant expressions.

C + + allows a variable to be declared as a constexpr type so that the compiler checks to see if the variable is a constant expression at compile-time. A variable declared as constexpr must be a constant expression. and initialization must be in constant expression.

For example:

const int a = 12; A is a constant expression

const int b = a+1; b is also a constant expression

const int c = GetSize (); c is not a constant expression, and the compiler cannot know the execution result of GetSize () at compile time.

constexpr int c = GetSize (); will be an error

A literal type is a type of result that can be obtained at compile time, including arithmetic types, references, and pointers. Custom classes, IO classes do not belong to this type. Objects of the literal type have strict requirements, and the literal type is the type of the object that has the constant expression property. For example, a literal constant is an arithmetic type. For references and pointers, the qualification is more restrictive. Not all pointers are constant expressions. Only those pointers that are determined at compile time to point to the address are constant expressions, which are referred to similarly. So nullptr, NULL, pointer to fixed address is a literal type. So how do you tell if the pointer is pointing to a fixed address?

The organization of the program in memory is a segment, with stack segments, data segments, and code snippets. For a data pointer pointing to data, the function pointer points to a code.

650) this.width=650; "Src=" http://img.blog.csdn.net/20160308231818111?watermark/2/text/ Ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/southeast "/>

For variables defined outside the function body, the pointer is constexpr, and such variables occupy the data segment, and the code snippet and the data segment size position are not changed when the program is run, so the compiler can determine the address point, which is constexpr. In addition, statically static variables defined inside the function also use a fixed address in the data area, so pointers are also constexpr. For variables that are defined inside a function, because of the need to open up memory space in the stack, and the situation of the stack depends on the program running state, such variables do not have a fixed address, the pointer is not constexpr. The code snippet does not change, and the function pointer is constexpr.


This article is from the "Wave" blog, make sure to keep this source http://zhaojia9290.blog.51cto.com/9429880/1748984

Literal and literal types of C + +

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.