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

             Bug #: 56537
           Summary: [C++11] lambda expression treats members as const
    Classification: Unclassified
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: martin.vgag...@gmx.net


The following code won't compile on GCC 4.7.2:

#include <vector>
template<typename T> struct Foo {
  std::vector<int> member;
  Foo() {
    auto f = [this]() {
#if 0
      this->
#endif
      member.push_back(42);
    };
  };
};
void bar() { Foo<int> foo; }

The compiler reports:

foo.cc: In lambda function:
foo.cc:9:26: error: no matching function for call to
‘std::vector<int>::push_back(int) const’
foo.cc:9:26: note: candidates are:
In file included from …/4.7.2/include/g++-v4/vector:65:0,
                 from foo.cc:1:
…/4.7.2/include/g++-v4/bits/stl_vector.h:881:7: note: void std::vector<_Tp,
_Alloc>::push_back(const value_type&) [with _Tp = int; _Alloc =
std::allocator<int>; std::vector<_Tp, _Alloc>::value_type = int] <near match>
…/4.7.2/include/g++-v4/bits/stl_vector.h:881:7: note:   no known conversion for
implicit ‘this’ parameter from ‘const std::vector<int>*’ to ‘std::vector<int>*’
…/4.7.2/include/g++-v4/bits/stl_vector.h:899:7: note: void std::vector<_Tp,
_Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = int;
_Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::value_type = int] <near
match>
…/4.7.2/include/g++-v4/bits/stl_vector.h:899:7: note:   no known conversion for
implicit ‘this’ parameter from ‘const std::vector<int>*’ to ‘std::vector<int>*’

But the "this" parameter in this case is a "Foo* const", not a "const
std::vector<int>*". There are several ways to avoid this problem: one can
enable the "this->" prefix currently disabled using the preprocessor. Or one
can make the whole lambda mutable. But as this appears to be a strange corner
case only encountered in the interplay of several templates, I doubt that this
is intended behaviour.

Reply via email to