C++ mutable


A specifier that can be applied to the declaration of a non-static, non-reference data member of a class. A mutable member of a class is not const even when the object is const.

class C {
    int x;
    mutable int times_accessed;
    C(): x(0), times_accessed(0) {
    int get_x() const {
        ++times_accessed; // ok: const member function can modify mutable data member
        return x;
    void set_x(int x) {
        this->x = x;

A second meaning for mutable was added in C++11. When it follows the parameter list of a lambda, it suppresses the implicit const on the lambda's function call operator. Therefore, a mutable lambda can modify the values of entities captured by copy. See mutable lambdas for more details.

std::vector<int> my_iota(int start, int count) {
    std::vector<int> result(count);
    std::generate(result.begin(), result.end(),
                  [start]() mutable { return start++; });
    return result;

Note that mutable is not a storage class specifier when used this way to form a mutable lambda.