Take a look at this code snippet:
int x = 3;
const int &y= x;
auto f = [y]() mutable{
y = 0;
};
clangs gives error:
> clang++-7 -pthread -std=c++17 -o main main.cpp
main.cpp:13:7: error: cannot assign to a variable captured by copy in a non-mutable
lambda
y = 0;
~ ^
1 error generated.
It works if capture by [y=y].
Why is this behavior? Is it that compiler deduct type as const int? If so why clang gives error about non-mutable lambda?
[update]
Seems the first part of the question already has an answer as pointed out by others: lambda capture by value mutable doesn't work with const &?
clang's error info seems to be a bug which is not discussed in that question. Should I delete the question?