使用默认捕获方式时,是通过副本捕获还是通过引用捕获(* this)?是同一回事吗? [英] When using either default capture mode, is this captured by copy or (*this) captured by reference? Is it the same thing?

查看:58
本文介绍了使用默认捕获方式时,是通过副本捕获还是通过引用捕获(* this)?是同一回事吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我看到下面的作品时,我感到有点困惑

I was a bit puzzled when I saw the following working

struct A {
    void g(){}
    void f(){
        [&](){ g(); }();
    }
};

,但随后我找到了 答案,其中相当详细地解释了其工作原理.

but then I found this answer which explains in quite deep detail how that works.

从本质上讲,无论您使用 [=] 还是 [&] ,它都可以按值捕获 .

In essence, it boils down to this is captured by value whether you use [=] or [&].

但是,在 cppreference 上,我阅读了以下内容

However, on cppreference I read the following

如果存在任何捕获默认值,则可以隐式捕获当前对象(* this).如果隐式捕获,则始终通过引用捕获它,即使捕获默认值为 = .

The current object (*this) can be implicitly captured if either capture default is present. If implicitly captured, it is always captured by reference, even if the capture default is =.

因此,我有两个公式似乎是等效的

So I have two formulations which seem to be equivalent

    不管我们使用 [=] 还是 [&]
  1. 均通过值捕获.li>不管我们使用 [=] 还是 [&] ,都会通过引用捕获
  2. (* this)
  1. this is captured by value whether we use [=] or [&].
  2. (*this) is captured by reference whether we use [=] or [&].

首先,上述两种说法的确完全等同吗?

First of all, are indeed the two formulations above perfectly equivalent?

我确实看到了两者之间的差异,即使实际上没有可能性,至少在它们看起来有用方面也是如此.

I do see a difference between the two, if not in what's practically possibile, at least in terms how useful they look.

公式1对我来说似乎很奇怪.为什么阻止我通过 const -reference捕获 this ?在性能方面,在两种情况下应该没有太大的区别,而且我总是可以执行 auto const&this = this; 在lambda之前,然后根据需要捕获 This .关于悬挂引用,如果我使用 this 关键字,那么我在对象类中,那么当对象位于对象类中时该如何死亡?

Formulation 1 seem just weird to me. Why preventing me from capturing this by const-reference? Performance-wise there shouldn't be any big difference in the two cases, and I can always do auto const& This = this; before the lambda, and then capture This by reference, if I want to. As regards dangling references, if I'm using the this keyword, I'm in the object class, so how can the object die while I'm inside it?

公式2似乎在保护我免受不必要的复制的影响,如果使用 [=] (* this)会发生不必要的复制>.此外,如果我真的想要,有一种捕获语法可以通过副本捕获(* this),即 [* this] .

Formulation 2, on the other hand, does seem to protect me from making unnecessary copies that would happen if (*this) was captured by value when using [=]. Furthermore, if I really want to, there's a capture syntax for capturing (*this) by copy, namely [*this].

推荐答案

第一种解释是C ++ 17之前的意图(编写答案时),但是第二是意图自(如最新的cppreference所反映).最初认为 this (指针)是捕获的主题,因此当然不可能通过引用来实现(因为

The first interpretation was the intent prior to C++17 (when that answer was written), but the second is the intent since (as reflected by the ever-current cppreference). Originally it was considered that this (the pointer) was the subject of the capture, and it is of course impossible to do so by reference (as pointed out; const auto &x=…; is not at all the same thing if it introduces a temporary). C++17 introduced capturing *this (by value), reinterpreting the existing capture [this] as being effectively [&*this] (which is not actually valid syntax).

C ++ 20继续过渡,不赞成

C++20 continues the transition, deprecating

struct A {
  auto f() {return [=] {return this;}}
};

因为它实际上并未按值捕获任何内容,反之则 ">"而不是多余的按值捕获所有内容,也按值捕获此代码".相反, [& this] 现在应该被认为是 redundant ,但尚未弃用(尚未).

since it doesn't actually capture anything by value, and conversely allowing [=,this] which is now taken to mean "capture everything by value except for *this" rather than the redundant "capture everything by value and also this by value". Conversely, [&,this] should now be considered to be redundant, but it hasn't been deprecated (yet).

这篇关于使用默认捕获方式时,是通过副本捕获还是通过引用捕获(* this)?是同一回事吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆