What is the use of mutable keyword in cpp?

  • #1
how does mutable keyword functions in cpp.
what are it's applications.
do using 'mutable' keyword is preferable?.
 

Answers and Replies

  • #2
jtbell
Mentor
15,755
3,963
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.
 
  • #3
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
686
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.
 

Related Threads on What is the use of mutable keyword in cpp?

  • Last Post
Replies
2
Views
3K
  • Last Post
Replies
0
Views
1K
  • Last Post
Replies
2
Views
12K
  • Last Post
Replies
3
Views
9K
  • Last Post
Replies
1
Views
1K
Replies
5
Views
866
Replies
4
Views
7K
Replies
1
Views
1K
Top