Which statement about std::unique_ptr is correct?
Correct answer
B. It is move-only: copying is deleted, and ownership is transferred with std::move
15 views
B. It is move-only: copying is deleted, and ownership is transferred with std::move
15 views
std::unique_ptr models exclusive ownership with no overhead over a raw pointer, so its copy operations are deleted and only moves exist, typically produced by std::make_unique in C++14. Returning one by value relies on move semantics, which is why factory functions return unique_ptr freely. Shared ownership needs std::shared_ptr and its reference-counted control block.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is B? Ask here and get an explanation.