[Bug c++/60755] New: lambda capturing `this` doesn't honor the const qualifier of the enclosing member function

filip.roseen at gmail dot com gcc-bugzilla@gcc.gnu.org
Fri Apr 4 00:05:00 GMT 2014


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60755

            Bug ID: 60755
           Summary: lambda capturing `this` doesn't honor the const
                    qualifier of the enclosing member function
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: filip.roseen at gmail dot com

Created attachment 32539
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=32539&action=edit
testcase.cpp

#include <iostream>

struct A {
  void f () const { std::cerr << "const "; }
  void f ()       { std::cerr << "non-const "; }

  void g () const {
    [this] { f (); this->f (); } ();
  }
};

int main () {
  A {}.g ();
}


-----------------------------------------------------------------

The above prints "non-const const", even though the type of `this`
should be `A const *` in both `f()` and `this->f()` inside the
anonymous lambda.

[ Note: `clang` will generate a binary which correctly outputs "const const" ]

-----------------------------------------------------------------

[expr.prim.lambda]p18 states:

  > If `this` is captured, each odr-use of `this` is transformed into an
  > access to the corresponding unnamed data member of the closure type,
  > cast (5.4) to the type of `this`.



More information about the Gcc-bugs mailing list