When the mutable
specifier is used in the declaration of a non-static data member, the data is mutable no matter whether the rest of the object is treated as const. With this fact, we may easily have the impression that mutable
specifier is the same kind of thing as the const
qualifier, which turns out to be not true. In fact, the language classifies mutable
specifier as a storage class specifier. This is quite counter-intuitive because mutable
does not specifies storage duration.
What are the rationales behind this design decision?
What would make it less logical than it would seemed if mutable
was a qualifier?
What are the advantages of making it a storage class specifier?
The mutable specifier on a class data member nullifies a const specifier applied to the containing class object and permits modification of the mutable class member even though the rest of the object is const (7.1.6.1).
– Mctyreconst
andvolatile
are part of the type.mutable
isn't. – Strotherconst
applies differently to each 'level' when there is indirection in the type, butmutable
is always "top-level". And it's nice to allowmutable int *p;
instead of requiringint * mutable p;
– Varipapa