I am attempting to update some code that hands off a new object. The goal is to manage it with a smart pointer. Boiled down, it looks much like this:
class X
{
shared_ptr<Y> yptr;
X() : yptr(Y::create(new Z())){}
};
class Y
{
Z* zptr;
static shared_ptr<Y> create(Z* zp)
{
if(!zp) return nullptr;
else return shared_ptr<Y>(new Y(zp));
}
Y(Z* zp) : zptr(zp){}
}
So far this seems to work:
class X
{
shared_ptr<Y> yptr;
X() : yptr(Y::create( std::move( std::make_unique<Z>(Z()) ) )){}
};
class Y
{
unique_ptr<Z> zptr;
static shared_ptr<Y> create(unique_ptr<Z> zp)
{
if(!zp) return nullptr;
else return shared_ptr<Y>(new Y(std::move(zp)));
}
Y(unique_ptr<Z> zp) : zptr(std::move(zp)){}
}
My question is, is the first std::move() (around make_unique) necessary? Visual Studio doesn't seem to mind either way. I'd rather have an accurate understanding before I start making similar changes elsewhere where performance is more critical.