Jump to content

[C++] [Intermediate/Advanced] Mutable


Recommended Posts

mutable is a little known C++ keyword used in the declaration of variables.

According to http://www.cppreference.com/wiki/keywords/mutable

?The mutable keyword overrides any enclosing const statement. A mutable member of a const object can be modified.?

This means that, instead of

double pie;

the variable pie can be declared as

mutable double pie

The difference between the mutabled and non-mutabled versions? Constant member functions can modify the mutabled one.

#include <iostream>

class Hello
    Hello() { _pv_nValue = 0; }
    const int GetValue() const
        return ++_pv_nValue;

    mutable int _pv_nValue;

void Do(const Hello& all)
    std::cout << all.GetValue() << std::endl;
    std::cout << all.GetValue() << std::endl;
    std::cout << all.GetValue() << std::endl;

int main(const int nArgCount, const char** pcArgs)
    Hello world;
    return 0;

This will output:




?But Jason,? you ask, ?Surely the whole point of constant member functions is to prevent the function altering the variables of the object.?

Indeed, you are correct...in theory. However, as many people know (like 99% of all physicists), theory and practice are two separate things.

Warning: Do NOT assume this means you can use mutable to weasel your way out of tricky situations and corners you coded yourself into...you can't. Well, you can but it's terrible practice and if you do it then more fool you. Think before you leap/code/whatever.

?So what purposes can mutables serve? you ask.

Well, they are several possible reasonable purposes, but most if not all of them you'll never encounter in all your time programming. Ever. This would be why the mutable keyword is little-known about, it's little-used.

One possible use is that perhaps, just perhaps, you wish to store how often a constant expression is called. What can you do? It's constant so can't modify a regular variable, so you're stuck here, right?

In the words of Lex Luthor: WRONG!

This is one of those situations mutable actually can be useful. As was illustrated above, you can use it to count the number of calls to a function.

Another use is practically given to you in the name. Mutexes. Sometimes constant functions may still need to lock down the thread. Perhaps it needs to do some calculations with the variables of the function and doesn't need the horror of the variables potentially changing mid-way through.

There are other potential, if unlikely, uses, but I'll leave thinking of or discovering those as an exercise for yourselves. I vaguely remember seeing mutable used at one or two points in the Loki library.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...