避免隐式捕获此内容? [英] Avoid implicit capture of this?

查看:61
本文介绍了避免隐式捕获此内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以编写一种类型,使其不能隐含地捕获自身内部带有lambda的this:

Is it possible to write a type such that it can't implicit capture itself this with a lambda inside itself:

[&]{}

我目前遇到的情况是,临时对象被用作工厂来创建另一个对象.在其中存在大量的lambda-避免错误地捕获数据成员将非常有用.

I currently have a situation where a temporary object is used as a factory to create another object. Inside this a lot of lambda's exist - it would be great to avoid capturing data members by mistake.

(请注意,这也与将来对代码进行过一点验证有关,因为这可能会导致一些很难发现的错误-另一个程序员可能会出现并不能很容易地实现这一点)

(note that this is also about future proofing the code a bit as this can result in some very hard to find bugs - another programmer can come along and not realize this quite easily)

(还请注意,在这种情况下用[&]捕获所有内容实际上是很棒的,因为我实际上确实想通过引用捕获其他所有内容,因为这可以正确绑定到工厂正在创建的所有新内容-是不正确的.)

(also note, it would actually be great to capture everything with [&] in this case as I actually do want to capture everything else by reference as this binds correctly to all the new stuff being created by the factory - [=] would be incorrect.)

推荐答案

我真的看不到禁止捕获this或用语言检测它的方法(我真的没有看到这样的类型特征) ,甚至具有更多的创造性用途).也许有用于此的工具,但我还没有发现任何东西.我认为您必须以某种方式考虑一下您的设计:

I don't really see a way to either forbid capturing this or detect it in the language (I don't really see a type trait for this, even with more creative use). Maybe there is tooling for this, but I haven't found anything. I think you have to overthink your design in some way:

  • 请勿使用[&]捕获.对于从不离开范围的本地lambda,最好通过引用捕获所有内容,但是就我个人而言,我绝不会将其用于从函数返回的任何lambda.
  • 将lambda构造委托为非成员函数(但这只是不要使用[&]",而需要执行额外的步骤)
  • 如果不想捕获this,为什么工厂甚至是对象?我真的不明白,那时您将在哪里使用它.重构整个东西,以使用静态成员函数甚至自由函数(C ++不是Java,具有自由函数的名称空间是一件好事).
  • 不是很健壮,但是有点奏效:对工厂成员变量(即F_*)使用一种非常容易理解的命名方案,然后如果您正在使用它,您将立即识别出来.
  • Do not capture with [&]. Capturing all by reference is nice for local lambdas that never leave the scope, but personally, I would never use it for any lambda that is returned from the function.
  • Delegate lambda construction to a non-member function (but this is only "Do not use [&]" with extra steps)
  • If you do not want to capture this, why is the factory even an object? I do not really see, where you would use that then. Refactor the whole thing to use a static member function or even a free function (C++ ain't Java, namespaces with free functions are a great thing).
  • Not so robust, but somewhat working: Use a very recognizable naming scheme for the factory member variables (i.e. F_*), then you will recognize immediately if you are using one.

(个人而言,我更喜欢第三点.在我看来,此类东西被过度使用了)

(Personally, I prefer the third point. In my opinion, classes are overused for such stuff)

好像有一个向llvm的拉请求,为此添加了一个警告(或者至少对于捕获的局部变量,我不确定如何用成员变量来解决).积极地看到了这一变化,但是却迷失了方向,因此似乎有人只需要重新定基础并对其进行审核,以使其保持最新状态. https://reviews.llvm.org/D24639

Seems like there was a pull request to llvm that adds a warning for this (or at least for captured local variables, I am unsure how that resolves with member variables). The change was seen positively but got lost on track, so it seems like someone just needs to rebase and review it to make it current. https://reviews.llvm.org/D24639

这篇关于避免隐式捕获此内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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