简体   繁体   English

提供给std :: unique_ptr的简单自定义删除器lambda:为什么使用by-reference capture default([&])over no capture([])?

[英]Simple custom deleter lambda supplied to std::unique_ptr: why use by-reference capture default ([&]) over no-capture ([])?

Background 背景

Cppreference:s section on std::unique_ptr shows the following demo for supplying a custom deleter to the unique_ptr instance: Cppreference: 关于std::unique_ptr部分显示了以下用于向unique_ptr实例提供自定义删除器的演示:

 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 其中D ,就这个问题而言,就像简单的自定义类型一样

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 be FunctionObject or lvalue reference to a FunctionObject or lvalue reference to function, callable with an argument of type unique_ptr<T, Deleter>::pointer Deleter必须是FunctionObject或lvalue引用的FunctionObject或lvalue函数引用,可以使用类型unique_ptr<T, Deleter>::pointer的参数调用

... ...

Member types 会员类型

pointer : std::remove_reference<Deleter>::type::pointer if that type exists, otherwise T* . pointerstd::remove_reference<Deleter>::type::pointer如果该类型存在,否则为T* Must satisfy NullablePointer . 必须满足NullablePointer

As a capture list of the deleter lambda in the example above, [&] is used. 作为上述示例中的删除器lambda的捕获列表,使用[&] 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]: 基于Cppreference: 关于lambdas部分 ,正如我所看到的,这个捕获列表在上面的删除器示例中的唯一影响是捕获“按引用的当前对象” [强调我的]:

[&] captures all automatic variables odr-used in the body of the lambda by reference and current object by reference if exists . [&]通过引用捕获lambda主体中使用的所有自动变量,如果存在 ,则通过引用捕获当前对象

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. 但正如我从上面所理解的那样,无论我们选择[&]还是[]作为lambda的捕获列表,都会使用对象的unique_ptr<T, Deleter>::pointer简单地调用提供的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). 我不明白为什么我们想要使用by-reference capture( 对象 ,这里的unique_ptr实例?)默认在这里,但我很确定我缺少必要的东西(因此问题) 。

Question

  • Is there any particular reason to use by-reference capture default ( [&] ) in the deleter lambda in the example above, as compared to simply using no-capturing ( [] )? 有没有特别的理由在上面的例子中使用删除器lambda中的by-reference capture default( [&] ),而不是简单地使用no-capture( [] )?

Here's what the standard says about the deleter of unique_ptr ( [unique.ptr.single]/1 ): 以下是关于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. 客户端提供的模板参数D应为函数对象类型(20.9),函数的左值引用或函数对象类型的左值引用,给定类型D的值d和类型为unique_ptr ::的值ptr ::指针,表达式d(ptr)是有效的,并具有处理指针适合该删除器的效果。

Judging by the above, both [] and [&] are perfectly valid. 从上述情况来看, [][&]都是完全有效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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