Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not non-const reference to temporary objects? [duplicate]

Possible Duplicate:
Does a const reference prolong the life of a temporary?
prolonging the lifetime of temporaries

C++ allows assignment of temporary objects only to const reference. It wont allow assignement of temporary objects to reference.

For example:

String& a = String("test");         // Error const String& a = String("test");   // Ok 

Everywhere I google for this result, i only see the following answers

  1. Modifying temporary objects would cause unidentifiable problems
  2. Modifying temporary objects is dangerous
  3. At some point of time, you will forget it is a temporary variable

It's been said, temporary objects vanishes after the statement. So you should not modify it.

If C++, is so keen in blocking modifying the temporary objects, it should have blocked reading the temporary objects right? If the temporary object is vanished, then there is no point in reading contents from there right? The possible cases, where a right can occur, could also involve the read too.

Then why its been blocking write alone and allowing read?

Please give me a solid c++ code explanation.

Please dont deviate the question by pointing some alternatives. Please give me solid answer with code why const int & is allowed and int & is not allowed for temporary objects.

One says && is there.. My question different.. ANother say,changing will not reflect.. Changing will not reflect even when it is const int & too. Eg: double a; Const int & i = a; a++; will not affect i..

like image 577
user738471 Avatar asked Dec 11 '12 19:12

user738471


People also ask

Why can't you make a non-const reference to a literal?

A non-const reference cannot point to a literal. You cannot bind a literal to a reference to non-const (because modifying the value of a literal is not an operation that makes sense) and only l-values can be bound to references to non-const.

Can a const reference refer to a non-const object?

No. A reference is simply an alias for an existing object.

Does const reference make a copy?

Not just a copy; it is also a const copy. So you cannot modify it, invoke any non-const members from it, or pass it as a non-const parameter to any function. If you want a modifiable copy, lose the const decl on protos .

Is temporary object created in return by reference?

Explanation: A temporary object is created when an object is returned by value. The temporary object is used to copy the values to another object or to be used in some way. The object holds all the values of the data members of the object.


2 Answers

The original case for not allowing references to temporaries was for function parameters. Suppose this was allowed:

void inc(double& x) { x += 0.1; }  int i = 0; inc(i); 

Why isn't i changed?

like image 148
Bo Persson Avatar answered Oct 01 '22 06:10

Bo Persson


If C++, is so keen in blocking modifying the temporary objects, it should have blocked reading the temporary objects right? If the temporary object is vanished, then there is no point in reading contents from there right?

No, reading the object is perfectly sensible. Just because it's going to vanish in the future doesn't mean reading the data now is pointless.

open_file(std::string("foo.txt")); 

std::string("foo.txt") is a temporary which will stop existing after the call to open_file() but the data it contains while it does exist matters very much.

The rational for not allowing temporaries bind to non-const references isn't actually some fundamental problem with writing to temporaries. In fact in many places C++ is perfectly happy to allow temporaries to be modified:

std::string("foo") = "bar"; 

It's just that the designers felt that it would cause a sufficient number of problems (probably due to the common idiom of 'out parameters') without enabling anything of similar value, so they simply made a design decision to prohibit temporaries binding to non-const references.

With rvalue references now you can do exactly what was prohibited before:

void foo(int &&output) {     output = 1; }  foo(2); 

This works fine, it's just not very useful.

like image 41
bames53 Avatar answered Oct 01 '22 06:10

bames53