I tried writing this class
#include <memory>
class ContainerUnique
{
public:
ContainerUnique(void);
~ContainerUnique(void);
private:
std::unique_ptr<UniqueElement> u;
};
Where UniqueElement is a POD class defined elsewhere. I now define the constructor body like this:
ContainerUnique::ContainerUnique(void)
{
auto tmp = new UniqueElement(1);
this->u(tmp); // u is a unique_ptr<UniqueElement>. Should this call compile?
}
And it complies without exceptions. Running the program I find that after the constructor of ContainerUnique has been called, u contains a null pointer.
Is this the intended behaviour? And what unique_ptr method am I actually calling?
This is a known problem with VS2010’s
unique_ptr. It publicly inherits from its deleter if it’s empty as an optimization (empty base optimization). The downside to the public inheritance is that all members of the deleter also become available members ofunique_ptr, in this case itsoperator()(T*)that deletes the pointer.The bug is fixed in VS2012’s library where the inheritance is changed to private.