Temporary variables in C + + cannot be reference parameters that are not const

Source: Internet
Author: User
temporary variables in C + + cannot be reference parameters that are not constTags: c + + compiler Initializationiostreamreferenceos 2009-02-05 17:21 4550 People read comments (3) Collection Report Category: Technology Sharing (34)

Quote: http://blog.baisi.net/?116670/viewspace-4407

Let's have a preview of 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); Compilation error, passing the temporary variable as a non-const reference parameter
G (A + B); OK, passing the temporary variable as const& is allowed
}

Before the two calls above, the value of the a+b will exist in a temporary variable, and when passing this temporary variable to F, the argument is int& in the declaration of F, not a constant reference, resulting in the following compilation error:
Const_ref.cpp:In function ' int main () ':
Const_ref.cpp:14:error:invalid Initialization of non-const reference of type '
   int& ; ' from a temporary of type ' int '
Const_ref.cpp:4: error:in passing argument 1 ' void f (int&) ' and in G (A+B), as defined by G parameter is const INT&, compiled through. The   problem is why a 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, changes, so the compiler will complain when passed as a very large reference. This explanation is possible on the issue of understanding that a temporary variable cannot be a non-const reference parameter, but is not accurate enough. In fact, a temporary variable can be evaluated as a left value (LValue), 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 the result:
5

4

The temporary variable was indeed assigned and succeeded.
Therefore, a temporary variable cannot be a non-const reference parameter, not because he is a constant, but because of a semantic restriction on the C + + compiler. If a parameter is passed in as a non-const reference, the C + + compiler has reason to think that the programmer will modify the value in the function, and the modified reference will work after the function returns. But if you pass a temporary variable as a non-const reference parameter, because of the particularity of the temporary variable, the programmer is not able to manipulate the temporary variable, and the temporary variable may be released at any time, so, generally speaking, it is meaningless to modify a temporary variable, accordingly, C + + The compiler added this semantic restriction that temporary variables cannot be used as non-const references, in an attempt to limit the potential errors of this very restrictive usage.
I don't know yet. OK, let's be straightforward, if you pass a temporary variable as a non-const reference parameter, on the one hand, in the function declaration, use a very variable reference to tell the compiler that you need to get the function to modify the object, but you do not give the variable a name, you discard the function of the modification result, The compiler can only say: "Big Brother, what are you doing, tell me the results to you, and so I give you the results, you throw directly, you this is not playing me?" "So the compiler rage won't let go." Let's get this over with.

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.