What is the use of mutable keyword in cpp?

  • Thread starter Thread starter sam topper.
  • Start date Start date
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 2K views
sam topper.
Messages
14
Reaction score
0
how does mutable keyword functions in cpp.
what are it's applications.
do using 'mutable' keyword is preferable?.
 
Physics news on Phys.org
A Google search for "C++ mutable" gave me this as the first hit:

http://www.highprogrammer.com/alan/rants/mutable.html

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

So if the above madness isn't what mutable is for, what is it for?

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.
 
sam topper. said:
how does mutable keyword functions in cpp.
what are it's applications.
do using 'mutable' keyword is preferable?.
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.