【问题标题】:C++ lambdas in Visual Studio 2013: capturing this pointerVisual Studio 2013 中的 C++ lambda:捕获此指针
【发布时间】:2014-02-22 05:11:16
【问题描述】:

看起来如果 lambda 在成员函数中定义并且 this 被捕获,那么在 lambda 中所有类成员都可以在不使用 this 关键字的情况下进行访问,我可以这样做

some_class_field = ....

而不是

this->some_class_field = ....

它是可移植的行为还是特定于 Visual Studio?

谢谢。

【问题讨论】:

    标签: visual-studio c++11


    【解决方案1】:

    预计:

    § 5.1.2 第 7 段

    lambda 表达式的复合语句产生函数调用运算符的函数体 (8.4), 但出于名称查找 (3.4) 的目的,确定 this (9.3.2) 的类型和值并转换 idexpressions 使用 (*this) (9.3.1) 将非静态类成员引用到类成员访问表达式中, 在 lambda 表达式的上下文中考虑复合语句。 [ 例子:

    struct S1 {
        int x, y;
        int operator()(int);
        void f() {
            [=]()->int {
                return operator()(this->x + y); // equivalent to S1::operator()(this->x + (*this).y)
            // this has type S1*
            };
        }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-02
      • 1970-01-01
      • 2019-07-07
      • 2018-08-24
      • 2015-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多