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++?