C + + Standard: C + + does not allow temporary values to be modified for any basic type (including pointers)

Source: Internet
Author: User

From the C + + standard library, see this sentence: C + + does not allow the modification of any basic type (including pointers) of the temporary value, think for a long time, really do not understand. Basic type Char,int,float and so on, what's the temporary value? For example, int a=2, what is the temporary value of this a? Can I interpret 2 as a temporary value? If 2 is a temporary value, obviously not, because you can't change the 2, then that sentence is meaningless. Temporary value first regardless, do not know how to find, search a lot, understand a general meaning, first look at a few examples from the Internet to find:

#include <iostream>usingnamespace  std; void Main () {    int4;    A5;     int *b;     4 ;     5 ;}

a++;
Suffix + + Returns a (temporary) rvalue whose value is the original value of a, and cannot be assigned a value. (Note that this is said to be the right value)
*b++ = 5;
The expression is * (b++) = 5;
This means adding 1 to the pointer before referencing.
b++, like a++, is a temporary right-hand value (whose value is the original value of B) and cannot be assigned, such as the following code is not valid:
b++ = ptr;
The b++ expression produces a temporary pointer value that, after the pointer is dereferenced (of course, the value must point to a valid location), is an lvalue , so:
*b++ = 5; is legal.

Look again at an example;

#include <iostream>using namespacestd;classmy{ Public: My (intj=0): I (j) {}inti; void operator= (my& out)//Here we redefine an assignment operator =, to prepare for the assignment below. {i= out. I; }}; My fun () {My temp (1); returnTemp//This returns a temporary variable for a class} intfun2 () {intA; returnA//this returns a normal int temporary variable }intMain () {my out(2); Fun ()= out;//Here you can assign a value to a temporary variable of a classFun2 () =3;//But here's an error for a variable of the normal int type.}

FUN2 () returns an rvalue that cannot be assigned, and can only be interpreted as such.

Then look at the issues mentioned in the C + + standard library:

vector<int> coll;.....sort (++coll.begin (), Coll.end ());
The book says it will fail to compile. I compiled it under VS2012 without problems.

Remember one point: the right value cannot be modified.

C + + Standard: C + + does not allow temporary values to be modified for any basic type (including pointers)

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.