Does unique_ptr::release() call the destructor?

前端 未结 5 871
后悔当初
后悔当初 2021-01-30 01:58

Is this code correct?

auto v =  make_unique(12);
v.release();     // is this possible?

Is it equivalent to delete of a

5条回答
  •  醉酒成梦
    2021-01-30 02:32

    Is this code correct?

    No. Use std::unique_ptr<>::reset() to delete the internal raw pointer:

    auto v =  std::make_unique(12);
    v.reset(); // deletes the raw pointer
    

    After that is done, std::unique_ptr<>::get() will return nullptr (unless you provided a non-nullptr parameter to std::unique_ptr<>::reset()).

提交回复
热议问题