473,847 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

refrence from return value ?

given a prototye:
string foo();

What would be the diffrence in calling it, and assigning
the return value in

string a = foo();
vs
string &a = foo();

(is the last one even legal?)
Jul 22 '05 #1
8 1325
"Nils O. Selåsdal" <NO*@Utel.no> writes:
given a prototye:
string foo();

What would be the diffrence in calling it, and assigning
the return value in

string a = foo();
vs
string &a = foo();


AFAIK the last version will lead to an segfault on the next try to read
from it. foo() returns a string, which is copied befor it is retured, so
a temporary object will be assignet to a reference, but the temporary
object will be dead after the assignement, and so the reference show to
anyway.

I did not try it out, but I think it will no work, but I'm no expert,
maybe my interpretation is wrong.

Kid regards,
Nicolas

--
| Nicolas Pavlidis | Elvis Presly: |\ |__ |
| Student of SE & KM | "Into the goto" | \|__| |
| pa****@sbox.tug raz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
Jul 22 '05 #2
Nicolas Pavlidis wrote:

"Nils O. Selåsdal" <NO*@Utel.no> writes:
given a prototye:
string foo();

What would be the diffrence in calling it, and assigning
the return value in

string a = foo();
vs
string &a = foo();


AFAIK the last version will lead to an segfault on the next try to read
from it. foo() returns a string, which is copied befor it is retured, so
a temporary object will be assignet to a reference, but the temporary
object will be dead after the assignement, and so the reference show to
anyway.

I did not try it out, but I think it will no work, but I'm no expert,
maybe my interpretation is wrong.


Well. Yes.
The guys writing the standard took provisions for that. The rule
is: The temporary lives as long as the reference to it lives.

But honestly: I wouldn't count on all compilers to implement this
correctly in all cases.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #3
Nicolas Pavlidis wrote:
"Nils O. Selåsdal" <NO*@Utel.no> writes:

given a prototye:
string foo();

What would be the diffrence in calling it, and assigning
the return value in

string a = foo();
vs
string &a = foo();

AFAIK the last version will lead to an segfault on the next try to read
from it. foo() returns a string, which is copied befor it is retured, so
a temporary object will be assignet to a reference, but the temporary
object will be dead after the assignement, and so the reference show to
anyway.


In this particular example the temporary object bound to the reference
will persist for the lifetime of the reference (see also the C++
Standard chapter 12.2). So no problem here.

Note that when a temporary object is passed by reference to a function
or constructor, the temporary object will only exist during the call.
Once the call has completed the temporary object will be destroyed, even
if the function or constructor has bound the temporary object to another
reference.

--
Peter van Merkerk
peter.van.merke rk(at)dse.nl
Jul 22 '05 #4

"Nicolas Pavlidis" <pa****@sbox.tu graz.at> wrote in message
news:2r******** *****@uni-berlin.de...
"Nils O. Selåsdal" <NO*@Utel.no> writes:
given a prototye:
string foo();

What would be the diffrence in calling it, and assigning
the return value in

string a = foo();
vs
string &a = foo();


AFAIK the last version will lead to an segfault on the next try to read
from it. foo() returns a string, which is copied befor it is retured, so
a temporary object will be assignet to a reference, but the temporary
object will be dead after the assignement, and so the reference show to
anyway.

I did not try it out, but I think it will no work, but I'm no expert,
maybe my interpretation is wrong.


The code is not legal because a temporary cannot be bound to a non-const
reference. But there are no temporary lifetime issues here I think so.

const string &a = foo();

would be perfectly legal and OK.

john

Jul 22 '05 #5
Karl Heinz Buchegger wrote:


Well. Yes.
The guys writing the standard took provisions for that. The rule
is: The temporary lives as long as the reference to it lives.


Don't you think that would contradict 12.2/2. I'm quoting the relevant
line only (the whole thing sounds terribly confusing anyway -- like
everything else in the Standard):

A temporary bound to the returned value in a function return statement
(6.6.3) persists until the function exits.

Regards,
Sumit.
--
Sumit Rajan <sumit DOT RAJAN AT gmail DOT com>
Jul 22 '05 #6
Sumit Rajan wrote:
Karl Heinz Buchegger wrote:


Well. Yes.
The guys writing the standard took provisions for that. The rule
is: The temporary lives as long as the reference to it lives.

Don't you think that would contradict 12.2/2. I'm quoting the relevant

^^^^^^^
Oops, sorry. That was 12.2/5 and not 12.2/2.

--
Sumit Rajan <sumit DOT RAJAN AT gmail DOT com>
Jul 22 '05 #7
Sumit Rajan wrote:

Karl Heinz Buchegger wrote:


Well. Yes.
The guys writing the standard took provisions for that. The rule
is: The temporary lives as long as the reference to it lives.
Don't you think that would contradict 12.2/2. I'm quoting the relevant
line only (the whole thing sounds terribly confusing anyway -- like
everything else in the Standard):

A temporary bound to the returned value in a function return statement

*************** **********
(6.6.3) persists until the function exits.


That's a different case.
We are not talking about what happens during the execution of the
return statement. The case under discussion happens after the function
has exited.

The above applies in situations like this

std::string foo()
{
return std::string();
}

Here a temporary string object is created for the return value. That temporary
exists until the function exits (has terminated). But that is very different
to what you actually do with the returned object:

const std::string& a = foo();

In foo()'s return statment a temporary was constructed. The return type of
foo() specifies that foo returns 'by value' thus a copy of that temporary
is to be made. And since that second temporary is bound to a reference, it
will live on as long as the reference exists. But the temporary created during
execution of the return statement is not the same temporary that gets bound
to the reference.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #8
Karl Heinz Buchegger wrote:
That's a different case.
We are not talking about what happens during the execution of the
return statement. The case under discussion happens after the function
has exited.

The above applies in situations like this

std::string foo()
{
return std::string();
}

Here a temporary string object is created for the return value. That temporary
exists until the function exits (has terminated). But that is very different
to what you actually do with the returned object:

const std::string& a = foo();

In foo()'s return statment a temporary was constructed. The return type of
foo() specifies that foo returns 'by value' thus a copy of that temporary
is to be made. And since that second temporary is bound to a reference, it
will live on as long as the reference exists. But the temporary created during
execution of the return statement is not the same temporary that gets bound
to the reference.


I guess I had misunderstood your initial statement. Sorry for the trouble.
--
Sumit Rajan <sumit DOT rajan AT gmail DOT com>
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1453
by: A.M | last post by:
Hi, Ho can I send a refrence type by value as a method parameter. Thanks, Alan
5
1413
by: KraftDiner | last post by:
I understand that everything in python is a refrence.... I have a small problem.. I have a list and want to make a copy of it and add an element to the end of the new list, but keep the original intact.... so: tmp = myList
161
7936
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.. (After reading that
2
1579
moishy
by: moishy | last post by:
I have a problem when passing by refrence using FOREACH(): foreach ($array as &$value) {if ($value <= 0) {$value+=12;}} It should have worked, but I got a error:
5
1630
by: giddy | last post by:
hi , i'm a C / C# programmer .. have'nt done C++, in C# .. . object instances of classes ( TextBox txt = new TextBox();) are reference types. Structs on the other hand are value types. In C++ i knw there are a few difference between classes and structs but i need to know if there are value or refrence types.
5
1726
eragon
by: eragon | last post by:
I am making a password script, i have most of it done, ill show you, but i hit a brick wall.... lol, I have it so when you log in it takes you to your main page, that works, but when you enter the wrong info, it's supposed to say "Invalid Username or Password" below the login boxes. (Using the document.write command), but to do this, i need to refrence the main script to get the yes or no if they logged in right.... i need help.... im using...
0
9727
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10645
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10335
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7880
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5719
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5909
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4524
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4116
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3160
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.