In an answer to Is it safe to store objects of a class which has an std::auto_ptr as its member variable in std::vector? I stated that a class that contained an auto_ptr could
Objects stored in containers are required to be "CopyConstructable" as well as "Assignable" (C++2008 23.1/3).
Your class tries to deal with the CopyConstructable requirement (though I'd argue it still doesn't meet it - I edited that argument out since it's not required and because it's arguable I suppose), but it doesn't deal with the Assignable requirement. To be Assignable (C++2008 23.1/4), the following must be true where t
is a value of T
and u
is a value of (possibly const
) T
:
t = u
returns aT&
andt
is equivalent tou
The standard also says in a note (20.4.5/3): "auto_ptr
does not meet the CopyConstructible and Assignable requirements for Standard Library container elements and thus instantiating a Standard Library container with an auto_ptr
results in undefined behavior."
Since you don't declare or define an assignment operator, an implicit one will be provided that uses the auto_ptr
's assignment operator, which definitely makes t
not equivalent to u
, not to mention that it won't work at all for "const T u
" values (which is what Earwicker's answer points out - I'm just pointing out the exact portion(s) of the standard).