gcc accepts #include <utility> struct foo { void func(); }; void bar(foo& v) { [v]() { static_assert(std::is_same_v<decltype(v), foo&>); [v]() mutable { static_assert(std::is_same_v<decltype(v), foo&>); // v.func(); }(); }(); } But uncomment the call and you get test.cc:12:20: error: passing ‘const foo’ as ‘this’ argument discards qualifiers [-fpermissive] 12 | v.func();
(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() {} | ^~~~ ```
I just tested a few compilers with the testcase from comment 1: * clang version 11.0.0 (https://github.com/llvm/llvm-project.git 0796b170fb3bf38e6cc4e59746120b37c9a9cd9f): Accepts it. * g++ (GCC) 10.1.1 20200507 (Red Hat 10.1.1-1): Rejects it: test.cc:17:14: error: passing ‘const foo’ as ‘this’ argument discards qualifiers [-fpermissive] * gcc (GCC) 9.3.1 20200528 Rejects it: test.cc:17:14: error: passing ‘const foo’ as ‘this’ argument discards qualifiers
Looks to be fixed on the trunk.
Dup of bug 94376 *** This bug has been marked as a duplicate of bug 94376 ***