Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2005-12-07 10:28:25


Yuval Ronen wrote:
> Hi.
> There's a shared_ptr constructor that accepts an auto_ptr<Y>&.
> Shouldn't
> it accept auto_ptr<Y> by value rather by non-const reference? It seems
> to me that the reference does not add anything,

The reference adds a strong exception safety guarantee. If the shared_ptr
constructor fails, the source auto_ptr is left intact. Pass by value would
have already zeroed out the source.

If you want to achieve the equivalent of pass by value, you can use an
explicit .release().

> but only causes a VC warning (level 4):
>
> ---
> warning C4239: nonstandard extension used : 'argument' : conversion
> from 'std::auto_ptr<_Ty>' to 'std::auto_ptr<_Ty> &'
> A reference that is not to 'const' cannot be bound to a non-lvalue
> ---
>
> when calling this constructor with a temporary auto_ptr.
> This warning is a good thing and I don't want to disable it.

This warning would actually be an error on a stricter compiler.

One happy day the compiler writers will bring us the rvalue reference, the
constructor will take auto_ptr &&, and all will be well with the world. :-)


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk