Why is unique_ptr::release not defined with [[nodiscard]]?

This is addressed in the paper that added [[nodiscard]] to many of the functions. From P0600R1 this is the remark about adding [[nodiscard]] to unique_ptr::release()

Titus: at Google 3.5% of calls would fail, but analysis showed that it was correct (but weird ownership semantics). See reflector email.


Because you've previously retrieved the pointer value and done stuff with it.

Simple approximation:

unique_ptr<someclass> ptr;
// ...
someclass *s = ptr.get();
if (s->are_we_there_yet()) {
    ptr.release();
    // finish up with s...
    s->close_garage_door();
    delete s;
}

Tags:

C++

Unique Ptr