[Bug c++/95368] gcc things that a lambda capture is both const and mutable

daniel.kruegler at googlemail dot com gcc-bugzilla@gcc.gnu.org
Wed May 27 16:35:56 GMT 2020


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95368

Daniel Krügler <daniel.kruegler at googlemail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |daniel.kruegler@googlemail.
                   |                            |com

--- Comment #1 from Daniel Krügler <daniel.kruegler at googlemail dot com> ---
(In reply to Rafael Avila de Espindola from comment #0)
> gcc accepts
> 
[..]

I found your code confusing, because the actual problem becomes visible only
under certain conditions. So lets make it a proper example, that shows the
problem immediately and lets ensure that we make it free from library
dependencies and provide all required information:

When using 

gcc HEAD 11.0.0 20200525 (experimental) 

and the following compiler flags

-Wall -Wextra -std=gnu++2a -pedantic 

to compile this code:

//---------
template<class, class>
constexpr bool is_same_v = false;

template<class T>
constexpr bool is_same_v<T, T> = true;

struct foo
{
  void func() {}
};

void bar(foo& v) {
  [v]() {
    static_assert(is_same_v<decltype(v), foo&>);
    [v]() mutable {
      static_assert(is_same_v<decltype(v), foo&>);
      v.func();
    }();
  }();
}

int main() {}
//---------

the program is rejected (but should be accepted) with the following
diagnostics:

```
prog.cc: In lambda function:
prog.cc:17:16: error: passing 'const foo' as 'this' argument discards
qualifiers [-fpermissive]
   17 |         v.func();
      |                ^
prog.cc:9:8: note:   in call to 'void foo::func()'
    9 |   void func() {}
      |        ^~~~
```


More information about the Gcc-bugs mailing list