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
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..
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.
No. A reference is simply an alias for an existing object.
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 .
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.
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With