简体   繁体   中英

Why doesn't unique_ptr::reset have overloads that take a deleter?

Is there a reason unique_ptr::reset doesn't have overloads that take a const deleter& and deleter&& to match its constructors that take those as a second argument?

The stored deleter in unique_ptr would be copy assigned or move assigned with the argument from reset . If the deleter is noncopyable or nonmovable, calling the corresponding overload of the reset wouldn't compile. This seems like it would be consistent behavior with the constructors.

I thought about adding that but you can get the equivalent functionality with a move assignment operator:

ptr = unique_ptr<T, D>(new T(another_value), D(another_state));

So I opted for not saying the same thing with reset in the interest of keeping the API reasonably small.

Update

And I live and learn...

The syntax can actually be much simpler than what I show above:

ptr = {new T(another_value), D(another_state)};

Because the deleter is stored in the object at construction. As the deleter type is a template argument, after construction there is no way to "convert" the class to use another one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM