public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/94624] New: Nested lambda mutable capture of outer lambda non-mutable capture is not allowed
@ 2020-04-16 17:32 cuzdav at gmail dot com
  2022-04-26 20:26 ` [Bug c++/94624] Assignment to nested mutable lambda's capture (of outer lambda non-mutable capture) " ppalka at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: cuzdav at gmail dot com @ 2020-04-16 17:32 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 94624
           Summary: Nested lambda mutable capture of outer lambda
                    non-mutable capture is not allowed
           Product: gcc
           Version: tree-ssa
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: cuzdav at gmail dot com
  Target Milestone: ---

An inner mutable lambda with a simple copy-capture of the outer lambda's
immutable variable is not allowed to be modified.  

This applies to all versions of g++ since mutable lambdas were introduced.

Live example
https://godbolt.org/z/RkMeYL


Full source code to reproduce:


////////////////////////////////////////////////
template <typename F> void foo(F const&) {}
template <typename F> void bar(F const&) {}

void bad() {
    int x = 5;
    foo([x]() { 
        bar([x]() mutable {
            x = 10;    // << modify local copy in mutable lambda should be ok 
        });
    });
}
////////////////////////////////////////////////


In lambda function:
8:15: error: assignment of read-only variable 'x'

    8 |             x = 10;

      |             ~~^~~~

Compiler returned: 1


Adding "mutable" to the outer lambda works around this, so somehow the
read-only behavior in the outer lambda is leaking into the inner lambda, even
though it is a simple copy and the underlying type is "int".

Additionally, clang accepts this all the way back to version 5.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c++/94624] Assignment to nested mutable lambda's capture (of outer lambda non-mutable capture) is not allowed
  2020-04-16 17:32 [Bug c++/94624] New: Nested lambda mutable capture of outer lambda non-mutable capture is not allowed cuzdav at gmail dot com
@ 2022-04-26 20:26 ` ppalka at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: ppalka at gcc dot gnu.org @ 2022-04-26 20:26 UTC (permalink / raw)
  To: gcc-bugs

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

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
   Target Milestone|---                         |12.0
                 CC|                            |ppalka at gcc dot gnu.org
         Resolution|---                         |DUPLICATE

--- Comment #1 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Looks like this is fixed for GCC 12 by r12-6065-g89cf57ea35d1e0 aka the fix for
PR94376.

*** This bug has been marked as a duplicate of bug 94376 ***

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-04-26 20:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-16 17:32 [Bug c++/94624] New: Nested lambda mutable capture of outer lambda non-mutable capture is not allowed cuzdav at gmail dot com
2022-04-26 20:26 ` [Bug c++/94624] Assignment to nested mutable lambda's capture (of outer lambda non-mutable capture) " ppalka at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).