The class TestConst has a member set of type std::function<void(double)>.
The setter method is marked as const but it still modifies the internal private variable m_value.
class TestConst {
public:
void setter(double v) const {
set(v);
}
private:
double m_value = 1;
std::function<void(double)> set = [this](double v) {m_value = v;};
};
Why does the compiler (g++ 10.2) allows that without any errors or warnings?
The capture of
thisisn't const, and the member isn't modified by*thisbut through an indirection.This is the same situation: