V
vissermc
Examine this example:
void foobar( shared_ptr <int> y )
{
if ( y.unique() ) { ... I can recycle this object for my own purpose,
efficient! ... }
else { .. too bad, I have to create a new object ... }
}
...main(..)
{
shared_ptr <int> x ( new int(3); )
foobar(x);
}
I suspect GCC (and other compilers?) will always call the destructor of
a passed variable after the last function is called.
In this case x will be destructed after foobar is called. This means
'foobar' will hit the else-case. But it would be better (more
efficient) if a shared_ptr will destruct before the actual call (there
is no need for main to hold x any longer).
The only solution I can see is: foobar(x.release());
Anyone who sees my problem? Is there a more clean/automatic solution to
this problem?
void foobar( shared_ptr <int> y )
{
if ( y.unique() ) { ... I can recycle this object for my own purpose,
efficient! ... }
else { .. too bad, I have to create a new object ... }
}
...main(..)
{
shared_ptr <int> x ( new int(3); )
foobar(x);
}
I suspect GCC (and other compilers?) will always call the destructor of
a passed variable after the last function is called.
In this case x will be destructed after foobar is called. This means
'foobar' will hit the else-case. But it would be better (more
efficient) if a shared_ptr will destruct before the actual call (there
is no need for main to hold x any longer).
The only solution I can see is: foobar(x.release());
Anyone who sees my problem? Is there a more clean/automatic solution to
this problem?