Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22035

Nested lambda and mutable keyword

$
0
0

Consider the following piece of code:

void f() {
    int a = 3;
    [=]() {
        [=] () mutable {
            a = 5;
        }();
    }();
}

It compiles on Clang (https://godbolt.org/z/IEXotM) but not on GCC (https://godbolt.org/z/xWXFe6). Error for GCC:

<source>: In lambda function:

<source>:5:15: error: assignment of read-only variable 'a'

    5 |             a = 5;

      |             ~~^~~

Compiler returned: 1

According to https://en.cppreference.com/w/cpp/language/lambda,

Optional sequence of specifiers.The following specifiers are allowed:

mutable: allows body to modify the parameters captured by copy, and to call their non-const member functions

And it seems like that Clang's behavior here is correct. Is this the case?


Viewing all articles
Browse latest Browse all 22035

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>