Why does this give compilation error?

D

doublemaster007

string & fun()
{
return "Heello";
}

If string temp = "Hello" is ok..

return "Heello"; should have created a temprory string object and that
should have been returned. instead it gives compiler error.
 
K

Kai-Uwe Bux

string & fun()
{
return "Heello";
}

If string temp = "Hello" is ok..

You mean, if you do:

string temp = "Heelo";
return ( temp );

???
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.

As for why the version with the temporary variable compiles: the variable is
not a temporary, therefore you can initialize a reference from there.
However, that string only lives to the end of fun(), and then the result is
a dangling reference, the use of which will give you undefined behavior.

I suggest, you ditch the "&" in the signature.
instead it gives compiler error.

Right.


Best

Kai-Uwe Bux
 
D

doublemaster007

string & fun()
{
return "Heello";
}
If string temp = "Hello" is ok..

You mean, if you do:

  string temp = "Heelo";
  return ( temp );

???
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.

As for why the version with the temporary variable compiles: the variable is
not a temporary, therefore you can initialize a reference from there.
However, that string only lives to the end of fun(), and then the result is
a dangling reference, the use of which will give you undefined behavior.

I suggest, you ditch the "&" in the signature.
instead it gives compiler error.

Right.

Best

Kai-Uwe Bux

THank you sooo much.!

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.
 
K

Kai-Uwe Bux

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, such references to temporaries can be
created under some circumstances. A member function could return that
reference, and it is legal to call non-const member functions on
temporaries. E.g.:

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.


Best

Kai-Uwe Bux
 
D

doublemaster007

(e-mail address removed) 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?

so would the below code work?? sorry i donot have a compiler to check
this now..

const string & fun()
{
return "Heello";
}
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?
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??
  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??
Best

Kai-Uwe Bux- Hide quoted text -

- Show quoted text -


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..
 
A

Alf P. Steinbach

* (e-mail address removed):
(e-mail address removed) 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.

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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top