C++ Logo

std-proposals

Advanced search

Re: Easier make_unique with deleter function

From: Phil Endecott <std_proposals_list_at_[hidden]>
Date: Tue, 24 Aug 2021 12:17:48 +0100
Thanks for all the replies.

Ville Voutilainen wrote:
> On Tue, 24 Aug 2021 at 00:46, Thiago Macieira via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
>> On Monday, 23 August 2021 09:56:58 PDT Ville Voutilainen via Std-Proposals
>> wrote:
>> > > Fundamentally unique_ptr
>> > > stores the deleter only in the type, which I think means the
>> > > deleter function can only ever be a template parameter:
>> > I don't quite follow. unique_ptr does store a deleter object, not just
>> > its type. It has
>> > http://eel.is/c++draft/unique.ptr.single.ctor#10 for exactly this purpose.
>>
>> True, but you may want to encode everything in the type, to avoid storing an
>> extra 8 bytes to make the indirect function call. That's likely the most
>> common use-case, as TIFF* gets freed with TIFFClose and FILE* with fclose.

Yes, this is what I was aiming for.

> I can do that without changing the client-side invocation of this
> function too much:
> https://wandbox.org/permlink/NBG9PbfxHbNniudX

Oh that's interesting. Basically if you use &TIFFClose as the
deleter it stores the function pointer, but if you write a trivial
lambda wrapper the deleter is an empty type and sizeof(unique_ptr)
== sizeof(raw pointer).

I think it's unfortunate that the less-efficient version is the
more concise, more obvious one.

More generally, I was wondering if a utility to convert a function
pointer to a function object would be useful - and actually I was a
bit surprised that there isn't already something. I'm sure there are
other situations where I've needed to supply a function object when
what I have is a function.


Regards, Phil.

Received on 2021-08-24 06:17:55