Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When have you used C++ 'mutable' keyword? [closed]

Tags:

c++

People also ask

What is the purpose of mutable keyword?

The keyword mutable is mainly used to allow a particular data member of const object to be modified. When we declare a function as const, the this pointer passed to function becomes const. Adding mutable to a variable allows a const pointer to change members.

What is the use of mutable in C++?

The mutable storage class specifier is used only on a class data member to make it modifiable even though the member is part of an object declared as const . You cannot use the mutable specifier with names declared as static or const , or reference members.

What is mutable and immutable C++?

A mutable object can be changed after it's created, and an immutable object can't. In Java, everything (except for strings) is mutable by default: public class IntegerPair { int x; int y; IntegerPair(int x, int y) { this.

What is difference between volatile and mutable in C++?

A variable marked mutable allows for it to be modified in a method declared const . A variable marked volatile tells the compiler that it must read/write the variable every time your code tells it too (i.e. it cant optimize away accesses to the variable).


Occasionally I use it to mark a mutex or other thread synchronisation primitive as being mutable so that accessors/query methods, which are typically marked const can still lock the mutex.

It's also sometimes useful when you need to instrument your code for debugging or testing purposes, because instrumentation often needs to modify auxiliary data from inside query methods.


I've used mutable in case of object caching results calculated from internal members:

class Transformation
{
    private:
        vec3 translation;
        vec3 scale;
        vec4 rotation;
        mutable mat4 transformation;
        mutable bool changed;
    public:
        Node()
        {
            [...]
            changed = false;
        }
        void set_translation(vec3 _translation)
        {
            translation = _translation;
            changed = true;
        }
        void set_scale(...) ...


        mat4 get_transformation() const
        {
            if(changed)
            {
                 // transformation and changed need to be mutable here
                 transformation = f(translation, scale, rotation); // This take a long time...
                 changed = false;
            }
            return transformation;
        }
};

void apply_tranformation(const Transformation* transfo)
{
    apply(transfo->get_transformation());
}

Google code search reveals a number of uses. For example, in an implementation of XTR cryptography, mutable members are used so that methods can return a reference to a result (preventing copies from being made).

For another example, Webkit uses it to lazily initialize member data (m_lineHeight).


I use mutable for class members that are initialized on demand, especially from a database or source external to the program. This allows the "getter" functions to create the object on demand otherwise it is a constant method.