Re: Why does this give compilation error?
* doublemaster007@gmail.com:
On Jan 24, 9:59 am, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
doublemaster...@gmail.com wrote:
On Jan 24, 9:29 am, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
doublemaster...@gmail.com wrote:
string & fun()
{
return "Heello";
}
[snip]
return "Heello"; should have created a temprory string object
Right.
and that should have been returned.
No. According to the signature, a _reference_ to that temporary should
have been returned. And that is the problem: non-const references cannot
be initialized from temporaries. See [8.5.3] for details.
[snip]
I knew below is dangrous..but i was just wondring why the other code
could not compile. I wasnt aware that refrences to temp object canot
be created.
Well, that is a little too condensed. While temporaries cannot be used to
initialize non-const references,
Can we have refrence to temporaries if it is const?? i mean can const
refrence be initialised by temporaroes?
Yes.
so would the below code work?? sorry i donot have a compiler to check
this now..
const string & fun()
{
return "Heello";
}
Nope, it's UB.
The current rules are simple in terms of what has to happen at the machine code
level -- no "magic" introduced by the compiler.
The above (which your compiler has to accept, but may warn about) means that a
temporary string instance is created in the function call's stack frame, a
reference to that instance is copied as result, and the instance is destroyed as
part of the function return. Thus, the reference is at this point invalid. About
the only thing it can be used for is to obtain an address in the stack.
such references to temporaries can be
created under some circumstances.
Can you tell me some circmstatnces where we can have no-cost refrence
to temporaries?
Not sure what circumstances Kai-Uwe was thinking of, but it seems reasonable
that the example he then immediately presented, and which you're quoting below,
may have something to do with it, don't you agree?
A member function could return that
reference, and it is legal to call non-const member functions on
temporaries. E.g.:
Confused...! Cant we call const member function on the tempo's?? If it
is const??
The basic rule is that is that a reference to non-const can't be bound to a
value (like, "int& r = 42"), because that reference could then be used to modify
the value.
std::vector<int>().operator=( some_int_vector );
returns a non-const reference to a temporary vector<int> with specified
value; and the temporary will live to the end of the expression.
As far as i know..tempo's will live till the outer scope ends..[not
till the end of expression] am i worng??'
Temporaries introduced in an expression are guaranteed destroyed at the end of
the full-expression (with two exceptions: used to initialize a declared object,
and used to initialize a reference).
Is there any way i can understand C++ specifications? Every time i
read..i find it diff to understand..so i read more books on C++ and
comes back..still the result is same..Are there any links, refereces
or documents which helps to parse specification easily?? I really
wanna understand that..pls help me..
There may be FAQ on this, and perhaps some web-sites dedicated to the issue.
Cheers, & hth.,
- Alf