Temporary variables in C + + cannot be used as non-const reference parameters

Source: Internet
Author: User

Try the following code:

#include <iostream>

using namespace Std;

void f (int &a)

{

Cout << "F (" << a << ") is being called" << Endl;

}

void g (const int &a)

{

cout << "g (" << a << ") is being called" << Endl;

}

int main ()

{

int a = 3, b = 4;

F (A + B); compile error, passing the temporary variable as a non- const reference parameter

G (A + B); OK, passing temporary variables as const& is allowed

}

Before the above two calls, the value of a+b will exist in a temporary variable, when the temporary variable is passed to F , because the declaration of F , the argument is int&, which is not a constant reference, produces the following compilation error:

In g (a+b) , because the parameters defined by G are const int&, the compilation is passed. the question is why the temporary variable must be a constant reference when it is passed as a reference parameter. Many people interpret this as a temporary variable that is constant, does not allow assignment, and changes, so when passed as a non-const reference, the compiler will give an error. This explanation is possible on the issue of understanding that temporary variables cannot be used as non- const reference parameters, but is not accurate enough. In fact, the temporary variable can be used as an lvalue (LValue) and be assigned, see the following code:

#include <iostream>

using namespace Std;

Class Ccomplex {

Friend Ccomplex operator+ (const Ccomplex &CP1, const Ccomplex &CP2);

Friend ostream& operator<< (ostream &os, const Ccomplex &CP);

Private

int x;

Public

Ccomplex () {}

Ccomplex (int x1) {

x = x1;

}

};

Ccomplex operator+ (const Ccomplex &CP1, const Ccomplex &CP2)

{

Ccomplex CP3;

cp3.x = cp1.x + cp2.x;

return CP3;

} ostream& operator<< (ostream &os, const Ccomplex &CP)

{

Os << cp.x;

return OS;

}

int main ()

{

Ccomplex A (2), B (3), C (4);

cout << (A + b) << Endl;

cout << ((a + b) = c) << Endl; temporary object as left value

return 0;

}

The above program compiles and runs, and the result is:

5

4

The TEMP variable is indeed assigned and succeeds.

Therefore, a temporary variable cannot be a non- const reference parameter, not because he is a constant, but because of a semantic limitation of the C + + compiler. If a parameter is passed in as a non- const Reference,theC + + compiler has reason to assume that the programmer will modify the value in the function, and that the modified reference will work after the function returns. However, if you pass in a temporary variable as a non- const reference parameter, the programmer cannot manipulate the temporary variable due to the particularity of the temporary variable, and the temporary variable can be released at any time, so, in general, It is meaningless to modify a temporary variable, whereby theC + + compiler joins the semantic restriction that the temporary variable cannot be used as a non- const Reference, and is intended to limit the potential error of this very rule usage.

Temporary variables in C + + cannot be used as non-const reference parameters

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.