10

Trying to compile the following code:

#include <functional>

void test() {

    int a = 5;

    std::function<void()> f = [a](){
        [a]()mutable{ // isn't it capture 'a' by copy???
            a = 13; // error: assignment of read-only variable 'a'
        }();
    };

}

gives the error: assignment of read-only variable 'a' error.

Changing the code by adding curly braces to a capturing:

#include <functional>

void test() {

    int a = 5;

    std::function<void()> f = [a](){
        [a{a}]()mutable{ // this explicitly copies a
            a = 13; // error: assignment of read-only variable ‘a’
        }();
    };

}

eliminates the compile error. I'm wondering why is it so? Isn't the first variant equivalent to the second?

This is when using g++ version 8.3.0 from Debian.

clang++ version 7.0.1 compiles it successfuly.

Bug in g++?

6
  • 1
    Is the original a local? Global? Jul 9, 2020 at 15:45
  • I tried with a local, haven't tried with global
    – igagis
    Jul 9, 2020 at 15:47
  • 1
    Making both lambdas mutable makes g++ accept the code. Weird.
    – chi
    Jul 9, 2020 at 16:03
  • in case of global a the error (or should it be a warning?) is valid, as globals do not have to be captured, those are accessible from within the lambda even without capturing
    – igagis
    Jul 9, 2020 at 16:05
  • @chi so, it is like g++ captures the a by reference in that case
    – igagis
    Jul 9, 2020 at 16:05

1 Answer 1

5

[C++11: 5.1.2/14]: An entity is captured by copy if it is implicitly captured and the capture-default is = or if it is explicitly captured with a capture that does not include an &. For each entity captured by copy, an unnamed non-static data member is declared in the closure type. The declaration order of these members is unspecified. The type of such a data member is the type of the corresponding captured entity if the entity is not a reference to an object, or the referenced type otherwise.

The type of a inside your mutable lambda is const int because it was captured by copy from a const int a of enclosing const lambda. Therefore, making both lambdas mutable solves this issue.

3
  • I think there's some wonkiness here. I think it's actually specified that the inner [a] is referring to the original int a = 5; not the data member of the closure type of the outer lambda. That is, in a lambda [a]() { use(a); }, use(a) is basically considered to reference the a that's in scope as if the lambda weren't there, and only then the lambda "intercepts" it. E.g. try checking if a is const in the outer (const) lambda. Then the inner capture list is referring to the non-const int a, and would capture non-const. GCC bug?
    – HTNW
    Jul 9, 2020 at 16:52
  • perhaps in that case the bug might be that decltype(a) is seeing outer a, rather than lambda's a, and both g++ and clang++ behave identically in this case, so it is either a bug in both compilers or some valid behavior we don't understand at the moment
    – igagis
    Jul 9, 2020 at 20:53
  • That's the thing: I'm fairly sure that there is no "lambda's a", according to the standard. The things inside a lambda refer to the things outside the lambda directly, and then the lambda intercepts them. So the inner capture list would refer to the outer a, make its own data member non-const, and then the outer lambda redirects the read of a used to initialize the inner lambda to its data member.
    – HTNW
    Jul 10, 2020 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.