What is the deal with temporary objects and links?

You can often read that you cannot bind a normal lvalue reference to a temporary object. Because of this, you can often see class A methods that take const A & as a parameter when they do not want to include copying. However, this design is completely legal:

double& d = 3 + 4; 

since it does not bind the temporary object 3 + 4 with the link d, but rather initializes the link with the object 3 + 4. As stated in the standard, only if the value does not refer to the type or link (or inherited), the won link is initialized using the object received from a temporary object using conversion or sth (i.e. another temporary object). You can see that in this case:

 int i = 2; double & d = i; 

This is illogical because I am not of type double and do not inherit it. However, this means that temporary persons can be linked to links - but is it really connected? Is it not creating a new object using the copy constructor with a temporary object as its parameter?

Therefore, as I think, the point of existence of methods that take const A & param instead of A & is not in the second case, such a method will not be able to take a temporary object of type A as a parameter (because it will), but because it includes a constructor copying (exactly as if the parameter was of type A). I'm right?

+4
source share
5 answers

First, as others have said, double & d = 3 + 4; non- legal C ++; if your compiler accepts it and claims to compile C ++, this is a compiler error. (Note that most C ++ compilers do not pretend to compile C ++ unless you give them special options, -std=c++98 in the case of g ++, for Example.)

Secondly, the motivation for this rule comes from experience. Consider the following example:

 void incr( int& i ) { ++ i; } unsigned x = 2; incr( x ); // Implicit conversion of unsigned to int // creates a temporary. std::cout << x << std::endl; // and x is still equal 2 here. 

The original link implementation did not have this limitation; You can initialize any link using a temporary character. Actual experience has shown that this is too error prone, so a constraint requiring a reference to const was introduced. (Around 1988 or 1989, so there is no excuse today for the compiler not to use it.)

Note also that it is often heard that the binding of the temporary to const refers to the temporary time of life. This is very misleading: the use of temporary link initialization lasts a temporary lifetime (with some exceptions), but the lifetime does not increase if this link is used to initialize other links, although temporary links are also associated with these.

+7
source

If you are worried about the meaning and purpose of const & vs. & in the lists of function parameters, I'm afraid that you bark the wrong tree, since it has little to do with temporary objects.

 void method( Object x ); 

This makes copy-construct a Object of the actual argument. Any changes made to x inside the function are lost, when the function ends, the function argument does not change.

But you do not want to pay the cost of copying.

 void method( Object & x ); 

This does not copy the Object construct from the actual argument, but x refers to the argument, that is, any changes made to x inside the function are actually performed by the argument itself.

But you do not want the callers of the method to wonder what might happen to their arguments.

 void method( const Object & x ); 

This does not copy-construct the Object from the actual argument, and x cannot be changed inside the function.

You do not pay for the constructor instance, and you explain to the caller that his argument will not be tampered with.

You cannot pass a temporary object as an argument to the second option (see unapersson answer), because there will be no changed object for the link, but since this function loudly declares that it will change the argument (since it declared a non-constant link), transfer the time argument is insensitive anyway.

+6
source

double& d = 3 + 4; is not completely legal; in fact, it will not be accepted by the standard compiler. The result of 3+4 is a temporary int type - as such, it can only be bound to a constant reference.

Linking links is indeed a must. There is no copying. This simply increases the lifetime of the temporary object.

+1
source
 void f( int & n ) { } int main() { f( 1 + 2 ); } 

No copy design is involved, but the temporary one will not be connected. Error with g ++:

invalid initialization of non-constant reference of type 'int &' from rvalue of type 'int'

0
source

A link can be initialized with an lvalue of the same type, and 3 + 4 can not be initialized with an lvalue. So this is not legal. The MS VC ++ compiler allows you to do something similar to your objects, but this is not standard.

0
source

All Articles