469,160 Members | 2,040 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,160 developers. It's quick & easy.

Is this legal? assigning return value to a const ref?

Hi,

Is this legal?

std::string foo()
{
std::string xyz = "FOO";

return xyz;
}
void bar()
{
const std::string s = foo();

// ... use s now
}

Does that force the xyz std::string that was returned (copy
constructed I assume) to be bound the the const reference and stay
around? Or is it going ot of scope since the object itself is not
being stored someplace explicitly named?

Thanks

Oct 29 '07 #1
10 3061
Hi,

OP here, in bar() it should be a const reference:

std::string foo()
{
std::string xyz = "FOO";
return xyz;
}
void bar()
{
const std::string& s = foo();

// ... use s now

}

Oct 29 '07 #2
flopbucket wrote:
Is this legal?
Yes (even considering your correction in the other post).
std::string foo()
{
std::string xyz = "FOO";

return xyz;
}
void bar()
{
const std::string s = foo();

// ... use s now
}

Does that force the xyz std::string that was returned (copy
constructed I assume) to be bound the the const reference and stay
around?
Yes, it does. The object returned from 'foo' is called "a temporary".
It's OK to bind a const reference to it.
Or is it going ot of scope since the object itself is not
being stored someplace explicitly named?
No, it isn't.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 29 '07 #3
On Oct 29, 8:45 pm, flopbucket <flopbuc...@hotmail.comwrote:
OP here, in bar() it should be a const reference:
std::string foo()
{
std::string xyz = "FOO";
return xyz;
}
void bar()
{
const std::string& s = foo();
// ... use s now
}
It's perfectly legal, but what's the point in using the const
reference (preferably written "std::string const&") rather than
a value?

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 30 '07 #4
It's perfectly legal, but what's the point in using the const
reference (preferably written "std::string const&") rather than
a value?
Well, in this particular case, the code was returning a reference to a
member variable. The internals of the class had to change for other
reasons and I was wondering if it was safe to just leave the calling
code as is, which used a const reference.

Thanks
Oct 30 '07 #5
flopbucket wrote:
>It's perfectly legal, but what's the point in using the const
reference (preferably written "std::string const&") rather than
a value?

Well, in this particular case, the code was returning a reference to a
member variable. The internals of the class had to change for other
reasons and I was wondering if it was safe to just leave the calling
code as is, which used a const reference.
It sounds like the code you posted didn't actually reflect what the
real code was doing. Perhaps you want to give your question another
shot and post the code before refactoring and after just to see if
somebody sees any problems in it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 30 '07 #6
On 2007-10-30 14:55, flopbucket wrote:
>It's perfectly legal, but what's the point in using the const
reference (preferably written "std::string const&") rather than
a value?

Well, in this particular case, the code was returning a reference to a
member variable.
No, it did not, foo() was declared to return a string, not a reference
to a string. That means that a copy will be created and you will then
make the const reference refer to that copy.
The internals of the class had to change for other
reasons and I was wondering if it was safe to just leave the calling
code as is, which used a const reference.
The code as you posted it is safe. However if you change foo() to return
a reference to a string instead you will have problems.

--
Erik Wikström
Oct 30 '07 #7
On Oct 30, 4:58 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-10-30 14:55, flopbucket wrote:
It's perfectly legal, but what's the point in using the const
reference (preferably written "std::string const&") rather than
a value?
Well, in this particular case, the code was returning a reference to a
member variable.
No, it did not, foo() was declared to return a string, not a reference
to a string. That means that a copy will be created and you will then
make the const reference refer to that copy.
I think he meant that it was in the past, but that it doesn't
any more---that in his actual code, foo() was a member function
that used to return a reference to a member variable, but now
returns a value.

In such cases, I'm still sceptical about the reference at the
call site. If I use a reference as a local variable, it's
because I want it to concretely refer to something; I want the
value to change, for example, if the member variable of the
class changes its value. Otherwise, I'll use a value. So the
fact that the function used to return a reference rather than a
value wasn't incidental (an "optimization"); it was an essential
part of the semantics of the class, which I was counting on.
The change in the class has probably broken my code.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 31 '07 #8

"James Kanze" <ja*********@gmail.comwrote in message
news:11*********************@k79g2000hse.googlegro ups.com...
On Oct 29, 8:45 pm, flopbucket <flopbuc...@hotmail.comwrote:
OP here, in bar() it should be a const reference:
std::string foo()
{
std::string xyz = "FOO";
return xyz;
}
void bar()
{
const std::string& s = foo();
// ... use s now
}
It's perfectly legal, but what's the point in using the const
reference (preferably written "std::string const&") rather than
a value?

==================

I tested this, and the output of the following program is indeed
One Two Unknown

#include <vector>
#include <iostream>
#include <string>

std::string Foo( int Val )
{
switch (Val)
{
case 1:
return "One";
break;
case 2:
return "Two";
break;
default:
return "Unknown";
break;
}

}

int main()
{
const std::string& One = Foo(1);
const std::string& Two = Foo(2);
const std::string& Three = Foo(3);

std::cout << One << " " << Two << " " << Three << "\n";
}

My question is, what is the lifetime of the returned string? The lifetime
of the references? I understand that normally the lifetime of a temporary
variable returned by a function is the statement it is called on, yet the
std::strings returned by Foo is beyond this.

Where do things std::strings reside, in what variable? I had always thought
that a reference was just a glorified pointer, but this code seems to
indicate more than this. The compiler seems to treat the std::strings
returned by Foo as being owned by the variables One, Two and Three, even
though they are references.

Dang, I thought I actually was starting to understand C++ yet there is
always something that throws me a suprise.
Oct 31 '07 #9
On 2007-10-31 16:26, Jim Langston wrote:
"James Kanze" <ja*********@gmail.comwrote in message
news:11*********************@k79g2000hse.googlegro ups.com...
On Oct 29, 8:45 pm, flopbucket <flopbuc...@hotmail.comwrote:
>OP here, in bar() it should be a const reference:
>std::string foo()
{
std::string xyz = "FOO";
return xyz;
}
>void bar()
{
const std::string& s = foo();
// ... use s now
}

It's perfectly legal, but what's the point in using the const
reference (preferably written "std::string const&") rather than
a value?

==================

I tested this, and the output of the following program is indeed
One Two Unknown

#include <vector>
#include <iostream>
#include <string>

std::string Foo( int Val )
{
switch (Val)
{
case 1:
return "One";
break;
case 2:
return "Two";
break;
default:
return "Unknown";
break;
}

}

int main()
{
const std::string& One = Foo(1);
const std::string& Two = Foo(2);
const std::string& Three = Foo(3);

std::cout << One << " " << Two << " " << Three << "\n";
}

My question is, what is the lifetime of the returned string? The lifetime
of the references? I understand that normally the lifetime of a temporary
variable returned by a function is the statement it is called on, yet the
std::strings returned by Foo is beyond this.
Yes, the temporary lives as long as the reference.
Where do things std::strings reside, in what variable? I had always thought
that a reference was just a glorified pointer, but this code seems to
indicate more than this. The compiler seems to treat the std::strings
returned by Foo as being owned by the variables One, Two and Three, even
though they are references.
My guess would be that, since the standard does not talk much about the
implementation of references, in this case the compiler simply creates
three strings on the stack, and the variables One, Two and Three refers
to them. In other words the code generated is the same as though One,
Two, and Three were const strings and not references.

--
Erik Wikström
Oct 31 '07 #10
On Oct 31, 12:26 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"James Kanze" <james.ka...@gmail.comwrote in message

news:11*********************@k79g2000hse.googlegro ups.com...
On Oct 29, 8:45 pm, flopbucket <flopbuc...@hotmail.comwrote:
OP here, in bar() it should be a const reference:
std::string foo()
{
std::string xyz = "FOO";
return xyz;
}
void bar()
{
const std::string& s = foo();
// ... use s now
}

It's perfectly legal, but what's the point in using the const
reference (preferably written "std::string const&") rather than
a value?

==================

I tested this, and the output of the following program is indeed
One Two Unknown

#include <vector>
#include <iostream>
#include <string>

std::string Foo( int Val )
{
switch (Val)
{
case 1:
return "One";
break;
case 2:
return "Two";
break;
default:
return "Unknown";
break;
}

}

int main()
{
const std::string& One = Foo(1);
const std::string& Two = Foo(2);
const std::string& Three = Foo(3);

std::cout << One << " " << Two << " " << Three << "\n";

}

My question is, what is the lifetime of the returned string? The lifetime
of the references? I understand that normally the lifetime of a temporary
variable returned by a function is the statement it is called on, yet the
std::strings returned by Foo is beyond this.

Where do things std::strings reside, in what variable? I had always thought
that a reference was just a glorified pointer, but this code seems to
indicate more than this. The compiler seems to treat the std::strings
returned by Foo as being owned by the variables One, Two and Three, even
though they are references.

Dang, I thought I actually was starting to understand C++ yet there is
always something that throws me a suprise.
"References to variables and references to constants are distinguished
because the introduction of
a temporary in the case of the variable is highly errorprone;
an assignment to the variable would
become an assignment to the - soon to disappear - temporary. No such
problem exists for references
to constants, and references to constants are often important as
function arguments."
TC++PL §5.5 P98

Oct 31 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Michael Sparks | last post: by
1 post views Thread by DonBot | last post: by
2 posts views Thread by Thomas Paul Diffenbach | last post: by
7 posts views Thread by Jim Ford | last post: by
13 posts views Thread by Kelvin Moss | last post: by
11 posts views Thread by Alberto Giménez | last post: by
9 posts views Thread by Peithon | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.