By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,864 Members | 1,277 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,864 IT Pros & Developers. It's quick & easy.

Returning Refs

P: n/a
const Thing &operator++(int)
{
Thing temp = *this;
operator++();
return temp;
}
Is this code robust ?
I get a compiler warning about returning a reference to a a local,
which I guess is because the compiler doesn't tell the difference
between this overload any any other function.
Thing x,y;
x = y++;
Is OK because it's thrown away, and it's a copy of non-incremented y
that gets assigned.

y++ = x;
Does things to my head, but can't happen because I'm returning a
const.
const_casting<doesn't
But could a (creatively stupid) user of my code do some harm ?

Mar 31 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Ev********@googlemail.com wrote:
const Thing &operator++(int)
{
Thing temp = *this;
operator++();
return temp;
}
Is this code robust ?
Not only it's not robust, it has undefined behaviour if the caller
of this function tries to use the reference returned.
I get a compiler warning about returning a reference to a a local,
which I guess is because the compiler doesn't tell the difference
between this overload any any other function.
Huh?
Thing x,y;
x = y++;
Is OK because it's thrown away, and it's a copy of non-incremented y
that gets assigned.
Huh? No. It's not OK because by the time you try _using_ the return
value from the operator++(int) the local object has been destroyed
already.
>
y++ = x;
Does things to my head, but can't happen because I'm returning a
const.
Not sure what you mean by "can't happen". The compiler should
flag that as an error since you try assigning to a const object.
const_casting<doesn't
Huh?
But could a (creatively stupid) user of my code do some harm ?
C++ is not fool-proof or malice-proof. Almost any program in C++
can be used in a way that would do some harm.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 31 '07 #2

P: n/a

<Ev********@googlemail.comwrote in message
news:11*********************@y80g2000hsf.googlegro ups.com...
const Thing &operator++(int)
{
Thing temp = *this;
operator++();
return temp;
}
Is this code robust ?
No. You are returning a reference to a temporary item. temp gets destroyed
when this mothod returns and so your'e returning a reference to something
that no longer exists.

Just return Thing, not Thing&
I get a compiler warning about returning a reference to a a local,
which I guess is because the compiler doesn't tell the difference
between this overload any any other function.
That pretty much makes no sense to me whatsoever. The compiler is warning
you that you are returning a reference to a local, because you are returning
a reference to a local. Your compiler is not lying to you, you are lying to
yourself.
Thing x,y;
x = y++;
Is OK because it's thrown away, and it's a copy of non-incremented y
that gets assigned.
No, that's not okay if you are reutrning a reference to x, because it is
also a local that will get destoryed when the method is finished executing.
y++ = x;
Does things to my head, but can't happen because I'm returning a
const.
const_casting<doesn't
But could a (creatively stupid) user of my code do some harm ?
Any user of your code would do some harm. Your code is wrong.
Apr 1 '07 #3

P: n/a
Jim Langston wrote:
<Ev********@googlemail.comwrote in message
news:11*********************@y80g2000hsf.googlegro ups.com...
>const Thing &operator++(int)
{
Thing temp = *this;
operator++();
return temp;
}
Is this code robust ?

No. You are returning a reference to a temporary item.
Just a clarification here... 'Temporary' has a very specific
meaning in C++. 'temp' here is an *automatic* object, and not
a C++ temporary. While it *is* temporary (hell, the whole
program is temporary since it only lasts while in computer's
memory), it has different lifetime than a _true temporary_.
temp gets
destroyed when this mothod returns and so your'e returning a
reference to something that no longer exists.
[..]
I have no problem with the rest of your assessment.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 1 '07 #4

P: n/a

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:yv******************************@comcast.com. ..
Jim Langston wrote:
><Ev********@googlemail.comwrote in message
news:11*********************@y80g2000hsf.googlegr oups.com...
>>const Thing &operator++(int)
{
Thing temp = *this;
operator++();
return temp;
}
Is this code robust ?

No. You are returning a reference to a temporary item.

Just a clarification here... 'Temporary' has a very specific
meaning in C++. 'temp' here is an *automatic* object, and not
a C++ temporary. While it *is* temporary (hell, the whole
program is temporary since it only lasts while in computer's
memory), it has different lifetime than a _true temporary_.
Yes, I guess You are returning a reference to an automatic variable that is
only temporary, it gets destroyed with then method returns. would help
clarify. I have to remember not to use temporary to mean temporary in
duration when it could be confused with C++'s temporary objects.
> temp gets
destroyed when this mothod returns and so your'e returning a
reference to something that no longer exists.
[..]

I have no problem with the rest of your assessment.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Apr 2 '07 #5

P: n/a
On Apr 1, 6:18 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Victor Bazarov" <v.Abaza...@comAcast.netwrote in message

news:yv******************************@comcast.com. ..
Jim Langston wrote:
<EvilOld...@googlemail.comwrote in message
news:11*********************@y80g2000hsf.googlegr oups.com...
const Thing &operator++(int)
{
Thing temp = *this;
operator++();
return temp;
}
Is this code robust ?
No. You are returning a reference to a temporary item.
Just a clarification here... 'Temporary' has a very specific
meaning in C++. 'temp' here is an *automatic* object, and not
a C++ temporary. While it *is* temporary (hell, the whole
program is temporary since it only lasts while in computer's
memory), it has different lifetime than a _true temporary_.
Since a temporary is always used (though it may be optimized away) to
return a result from a function call, the original response could have
been:

"No. You are returning a temporary bound to a reference to a local
object."

The trouble with binding a temporary to a reference (when the
referenced object is local) is simply this: as soon as the local
object goes out of scope - the lifetime of the temporary ends as well.
Therefore (and as others have already noted) the actual object as
returned to the caller is no longer a valid object.

Greg

Apr 2 '07 #6

P: n/a
Greg Herlihy wrote:
On Apr 1, 6:18 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"Victor Bazarov" <v.Abaza...@comAcast.netwrote in message

news:yv******************************@comcast.com ...
>>Jim Langston wrote:
<EvilOld...@googlemail.comwrote in message
news:11*********************@y80g2000hsf.google groups.com...
const Thing &operator++(int)
{
Thing temp = *this;
operator++();
return temp;
}
Is this code robust ?
>>>No. You are returning a reference to a temporary item.
>>Just a clarification here... 'Temporary' has a very specific
meaning in C++. 'temp' here is an *automatic* object, and not
a C++ temporary. While it *is* temporary (hell, the whole
program is temporary since it only lasts while in computer's
memory), it has different lifetime than a _true temporary_.

Since a temporary is always used (though it may be optimized away) to
return a result from a function call, [..]
Uh... Wrong.

int& foo() {
static int blah = 42;
return blah;
}

*What* temporary?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 2 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.