加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

关于默认捕获模式的限制和C lambda表达式中的“this”

发布时间:2020-12-16 06:00:51 所属栏目:百科 来源:网络整理
导读:我想知道为什么= capture-default模式禁止在C lambda表达式的捕获列表中. 那是, [=,this]{ }; // error[,this]{ }; // OK 这由C 11 5.1.2 / 8规定. If a lambda-capture includes a capture-default that is ,the identifiers in the lambda-capture shall n
我想知道为什么= capture-default模式禁止在C lambda表达式的捕获列表中.

那是,

[=,this]{ };  // error
[&,this]{ };  // OK

这由C 11 5.1.2 / 8规定.

  • If a lambda-capture includes a capture-default that is &,the
    identifiers in the lambda-capture shall not be preceded by &.
  • If a lambda-capture includes a capture-default that is =,the
    lambda-capture shall not contain this and each identifier it contains
    shall be preceded by &.

问:这个规则有什么理由或背景故事吗?

解决方法

这只能通过复制而不是通过引用捕获.即使您只指定[&],如果使用odr,则可以通过复制隐式捕获这一点.因此,[=,this]是一个错误,因为=已经隐含地通过副本捕获这个,而&在[& this]中,通过引用表示捕获,并不隐含地捕获此内容(除非是使用odr)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读