What's really going on here? I thought you weren't able/supposed to copy unique_ptr's however the following code compiles and runs properly:
std::unique_ptr<SomeObject> CreateObject()
{
return std::unique_ptr<SomeObject>(new SomeObject);
}
// Useage
auto MySomeObject = CreateObject();
Is the unique_ptr's move method being invoked? If so, is there a way to actually create the unique_ptr inside the function and return it without the object getting destroyed when the function scope exits?
I would rather not return the actual pointer then turn it into a unique_ptr as to force usage of unique_ptrs for objects returned by this function. Nor would I want to use shared_ptr's only to avoid the issue proposed in this question.
This is a very performance critical area of the application and I'm afraid extra overhead may be created here.
unique_ptr, it's moving it. The object is therefore not destroyed untilMySomeObjectis destroyed. Also, I'd be surprised if the above yields any overhead at all, since the RVO will very likely make sure thatMySomeObjectis initialized directly without calling any move-ctor.CreateObjectis part of C++14 and calledstd::make_unique.