
It’s an interesting and, on the face of it, obviously useful idea. What is required, more than simply creating a type-erased deleter? Isn’t this simply a partial specialisation of std::unique_ptr?
On 21 Mar 2017, at 08:04, Andrey Davydov via Boost
wrote: It is widely known that deleter type is part of std::unique_ptr type. It is the most effective decision, but sometimes not very convenient. The most important use cases (for me) when std::unique_ptr
doesn't work are following: 1. unique_ptr to incomplete class struct MyClass; unique_ptr<MyClass> create(); auto ptr = create(); // compilation error 2. upcast to base without polymorphic destructor struct Base { // ... // no virtual ~Base() here }; struct Derived : Base { // ... }; unique_ptr<Base> ptr = make_unique<Derived>(); // compiles by leaks!
3. unique_ptr with non-default deleter unique_ptr
open_file(std::string const & path) // works but looks ugly, why should the fact that fclose returns `int` be visible from the signature of the function `open_path`? { return { std::fopen(path, "r"), &std::fclose }; } Of course, all this examples could be fixed if unique_ptr would be replaced by shared_ptr, but the semantic of the shared ownership is not desirable often. I have implemented smart pointer with semantics of unique ownership and type erased deleter (https://github.com/AndreyG/unnamed). It has size of 3 pointers and it doesn't require additional memory allocations for the case when deleter is empty class (for example, std::default_delete or non-capturing lambda) and for the case when deleter is known at compile time function (for instance, fclose). The are some simple examples of usage and tests in the repo.
Why don't just use unique_ptr
>? The main reason is that due to the small object optimization std::function has rather big size. For instance, sizeof(unique_ptr >) == 72 for MSVC x64! Results for the other platforms can be found in the repo README. Does it seem useful?
-- Andrey Davydov
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost