2 ms·
This doesn't help to solve the original problem of avoiding the copy. Your calls to big_object_ptr.reset(...) will cause the BigObject copy constructor t
by mgraczyk 11y ago
This doesn't help to solve the original problem of avoiding the copy. Your calls to
big_object_ptr.reset(...)
will cause the BigObject copy constructor to be called. The object pointed to by unique_ptr does not have the same storage as the objects "foo" and "bar". At the very least, you need to do
big_object_ptr.reset(std::move(foo));
which will cause foo to be moved into the unique_ptr's storage.
In code like this, you should just use make_unique, in which case the BigObject is constructed in place, and unique_ptr can be RVO'ed:
if (...) {
return make_unique<BigObject>(...);
} else ...
- thrownaway2424 11y agoYeah I realized after the fact that the example is pointless, but I was trying to adhere to the original post, which is also pointless. Why would you construct two BigObject when you know you will discard one of them unused? It's dumb.