Cppreference:s section on std::unique_ptr
shows the following demo for supplying a custom deleter to the unique_ptr
instance:
std::unique_ptr<D, std::function<void(D*)>> p(new D, [&](D* ptr) { std::cout << "destroying from a custom deleter...\n"; delete ptr; });
Where D
, for the purpose of this question, is just as simple custom type, say
struct D
{
D() { std::cout << "D CTOR\n"; }
~D() { std::cout << "D DTOR\n"; }
};
Moreover, the reference above states the following type requirement on the deleter:
Type requirements
Deleter
must beFunctionObject
or lvalue reference to aFunctionObject
or lvalue reference to function, callable with an argument of typeunique_ptr<T, Deleter>::pointer
...
Member types
pointer
:std::remove_reference<Deleter>::type::pointer
if that type exists, otherwiseT*
. Must satisfyNullablePointer
.
As a capture list of the deleter lambda in the example above, [&]
is used. Based on Cppreference:s section on lambdas, as I see it, the only effect of this capture list in the deleter example above would be to capture the "current object by reference" [emphasis mine]:
[&]
captures all automatic variables odr-used in the body of the lambda by reference and current object by reference if exists.
But as I understand it from above, the supplied lambda will be simply called with the object's unique_ptr<T, Deleter>::pointer
, no matter if we choose [&]
or []
as capture list to the lambda. I don't understand myself why we'd want to use by-reference capture (of the object, which is the unique_ptr
instance here?) default here, but I'm pretty sure I'm missing something essential (hence the question).
[&]
) in the deleter lambda in the example above, as compared to simply using no-capturing ([]
)?Here's what the standard says about the deleter of unique_ptr
([unique.ptr.single]/1):
A client-supplied template argument D shall be a function object type (20.9), lvalue-reference to function, or lvalue-reference to function object type for which, given a value d of type D and a value ptr of type unique_ptr::pointer , the expression d(ptr) is valid and has the effect of disposing of the pointer as appropriate for that deleter.
Judging by the above, both []
and [&]
are perfectly valid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With