Why is mutable specifier classified to be storage class specifier, but not a qualifier?
Asked Answered
H

1

15

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?

Horary answered 8/5, 2016 at 6:10 Comment(5)
Your question is directly answered in [dcl.stc] of N4140: 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).Mctyre
const and volatile are part of the type. mutable isn't.Strother
An implementation is permitted to put a const object in read only memory - but not the mutable members, obviously. So if you squint a bit it is not totally unrelated to storage.Unmannered
@user6292850 No, it does not, utterly not.Horary
const applies differently to each 'level' when there is indirection in the type, but mutable is always "top-level". And it's nice to allow mutable int *p; instead of requiring int * mutable p;Varipapa
B
11

cv-qualifiers modify/restrict the object's semantics; mutable does not. It being part of the type would be superfluous in practically all scenarios, while necessitating more paragraphs about pointer conversions etc.

Thus it was decided to make it a decl-specifier that isn't a type-specifier. Since const can cause an object to be put into write-protected memory, mutable can cause an object not to be put into such storage, suggesting it is a storage-class-specifier.

Banerjee answered 8/5, 2016 at 13:24 Comment(1)
I suppose a hypothetical mutable int C::* (with mutable being part of the type pointed to) could be used to change a const C object's mutable int subobject via the pointer-to-member syntax, but certainly the cost is far greater than the benefit.Strother

© 2022 - 2024 — McMap. All rights reserved.