Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why does this give compilation error?

Reply
Thread Tools

Why does this give compilation error?

 
 
doublemaster007@gmail.com
Guest
Posts: n/a
 
      01-24-2009
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.
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      01-24-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> 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
 
Reply With Quote
 
 
 
 
doublemaster007@gmail.com
Guest
Posts: n/a
 
      01-24-2009
On Jan 24, 9:29*am, Kai-Uwe Bux <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > 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.
> string temp = "Heelo";
> return ( temp );

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      01-24-2009
(E-Mail Removed) wrote:

> On Jan 24, 9:29*am, Kai-Uwe Bux <(E-Mail Removed)> wrote:
>> (E-Mail 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, 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
 
Reply With Quote
 
doublemaster007@gmail.com
Guest
Posts: n/a
 
      01-25-2009
On Jan 24, 9:59*am, Kai-Uwe Bux <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > On Jan 24, 9:29*am, Kai-Uwe Bux <(E-Mail Removed)> wrote:
> >> (E-Mail 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..
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-25-2009
* (E-Mail Removed):
> On Jan 24, 9:59 am, Kai-Uwe Bux <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>>> On Jan 24, 9:29 am, Kai-Uwe Bux <(E-Mail Removed)> wrote:
>>>> (E-Mail 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.


>> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why oh why does this NOT give a compile error? Paul Melis C Programming 13 11-07-2007 12:06 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Why wouldn't this line of give give the address of the pointer? grocery_stocker C Programming 10 05-25-2005 03:05 AM
Give us 3 minutes; we give you the whole library lib Computer Support 0 01-27-2005 07:52 AM



Advertisments