Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

What is the use of mutable keyword in cpp?

  1. Oct 2, 2013 #1
    how does mutable keyword functions in cpp.
    what are it's applications.
    do using 'mutable' keyword is preferable?.
  2. jcsd
  3. Oct 2, 2013 #2


    User Avatar

    Staff: Mentor

    A Google search for "C++ mutable" gave me this as the first hit:


    The first part of this page is a rant about misuse of "mutable." To see what it should actually be used for, scroll down to the part that begins with

    I've never used "mutable" myself, but I remember reading about it once or twice before.

    Some of the other pages in that Google search might also be useful.
  4. Oct 2, 2013 #3

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    In most places it's highly unpreferable. It's usually a sign of a bad design. Many organizations make mutable a forbidden keyword. Programmers can still use it *if* they get a waiver to that rule. To obtain that waiver they need to have a very good reason why they need to use that keyword.

    One common usage is for a mutex. If a waiver is needed, it's almost automatic in the case of a mutex. But then they'll poke deep into your code to see if you are using the mutex properly. They'll question why you need to be thread-safe, since the project already does that for you. They'll look to see if your code is subject to deadlock and priority inversion.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook