Does unique_ptr::release() call the destructor?

前端 未结 5 866
后悔当初
后悔当初 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:49

    it may be a bit tricky for arbitrary types:

      unique_ptr v = get_me_some_foo();  // manages the object
      Foo * raw = v.release();        // pointer to no-longer-managed object
      delete raw;
    

    is almost correct.

      unique_ptr v = get_me_some_foo();  // manages the object
      Foo * ptr = v.release();        // pointer to no-longer-managed object
      v.get_deleter() ( ptr );
    

    this one would be correct in all situation; there may be a custom deleter defined on type Foo, but using the deleter returned by the unique_ptr object is good for all cases.

提交回复
热议问题