Re: Defeating (N)RVO
ThosRTanner skrev:
[snip]
I thought I'd seen articles on writing error codes that throw if you
don't access the value?
Anyway, wouldn't you get what you wanted if your copy constructor set
that the thing being copied had been accessed. Thus
class cheater
{
private stuff stuff;
private bool referenced;
public:
cheater(cheater &x) : stuff(x.stuff), referenced(false)
{
x.referenced = true;
}
}
With usual warnings about throwing from destructors, etc, etc.
Forgive me if I'm missing something obvious - I've had many years to
practice and I'm getting quite good at it now.
We went into the same school!
My cheater also has a
cheater& operator= (cheater &x) {... x.referenced = true;}
(and referenced is mutable), but otherwise it is exactly that idea i
had.
The problem is that if i create a cheater in a function and detect an
error, I will normally pass the error ... cheater... to the caller. If
RVO does not kick in all is fine. But if not, the callee will see the
object as having been referenced:
cheater somefunc()
{
cheater res = otherfunc();
if (res.isok()) do_some_more_stuff;
return res;
}
[NRVO means that for
cheater res2 = somefunc()
::res2 and somefuncs::res are effectively aliases and res2 will be
marked as referenced in the line if (res.isok()) ....]
/Peter
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]