Essence of C/C ++ left-value conversion (3) Left-value conversion from left to right

Source: Internet
Author: User

Essence of C/C ++ left

(3) Conversion of left values

 

1. Conversion from left to right

 

Let's take a look at an example:

 

Int I = 10;

 

The compiler opens a space in the memory with a sizeof (INT) byte. It initializes the space with an integer of 10 and names it as I. I belongs to the left value. When I is placed in the following expression:

 

I + 1;

 

Because the + binary operator only requires the right value, the I symbol is not required by it. In this case, the compiler extracts the integer 10 from the object represented by I and submits it to the + operator for calculation. The process of retrieving integer 10 from the object represented by I is a conversion process from left to right. In this process, the original left value I is replaced by the right value 10. However, the nature of I is not changed, and I is still a left value, some people will misunderstand that I has become a right value, and the conversion result is understood as a change in nature, which is actually a mistake of putting the cart before the horse upside down.

If you change the definition of I to the CV restricted form, for example:

 

Const int I = 10;

 

When I + 1 is calculated, does the right value of the I conversion carry a const? The so-called CV refers to the const and volatile modifiers. In C, the conversion result from the left value to the right value does not have the CV restriction form, even if the left value is CV restricted, that is, there is no right value restricted by the CV in C; c ++ is slightly different. It allows right-value class objects with limited CV, but the built-in type of the right-value is the same as that of C.

Because the non-referenced return value of a function belongs to the right value, if the function returns a built-in type with CV modification, This CV modification will be ignored. See the following code:

 

Const int Foo (void );

Int I = Foo ();

 

Although the return type of foo is const int, it gives the value of I to the int type rather than the const int type.

Because of the existence of such conversion results from the left value to the right value, we can use a left value restricted by CV to assign or initialize a left value not restricted by CV, for example:

 

Const int I = 10;

Int J;

J = I;

 

Although I is restricted by CV, the right value of the I Conversion does not have a CV, so it can be successfully assigned to J without CV restriction.

On the other hand, for the same reason, the CV modifier of the form parameter does not constitute a function overload condition of C ++, as shown below:

 

Int Foo (int I );

Int Foo (const Int J );

 

The above two Foo functions are repeated declarations of the same function, not heavy loads, because no matter whether the CV is restricted integers, they can be used as real parameters of I and J. The C ++ standard is like this:

 

13.1 overloadable declarations

 

Parameter declarations that differ only in the presence or absence of const and/or volatile are
Equivalent. That is, the const and volatile type-specifiers for each parameter type are ignored
When determining which function is being declared, defined, or called.

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.