Returning Refs

E

EvilOldGit

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

Victor Bazarov

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
 
J

Jim Langston

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

Victor Bazarov

Jim said:
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
 
J

Jim Langston

Victor Bazarov said:
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
 
G

Greg Herlihy

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
 
V

Victor Bazarov

Greg said:
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
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top