I have a class that contains a pointer to a constant VARIANT value outside the class, but sometimes I want to change this pointer to refer to a VARIANT member object of the class itself.
Most instances of this class will be const, so I have to declare the pointer as mutable.
In Visual C++ this code seems to do what I want:
VARIANT mutable const* m_value;
However, since mutable is meant to be a property of the pointer and not the pointee, I would think this to be the correct syntax:
VARIANT const * mutable m_value;
Similar to how you define a constant pointer (and not a pointer to a const object). Visual C++ does not accept this variant though.
warning C4518: 'mutable ' : storage-class or type specifier(s) unexpected here; ignored
Is Visual C++ right, or am I missing something? Could another more standard-conformant compiler behave differently?
Comeau online seems to agree with VC++ here.
And it also makes sense! A class member can only be mutable once and there is no such thing as a non-const pointer to a mutable const object. "Mutable const object" doesn't make sense.
You should put the mutable
in front of your declaration, as it is in the same area as, for example, static
:
class A {
static int const* m_p1; // static modifiable pointer to a const object;
mutable int const* m_p2; // mutable pointer to a const object
...
mutable int *const m_p3; // DOES NOT MAKE sense
m_p3
does not make sense - you declare the member as "always mutabel" and as "always const" at the same time.
VC++ is right. In this case mutable
is a storage-class-specifier, like static
, extern
and register
. Just like
int const* static foo;
won't compile, as a specifier must appear at the beginning of a declaration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With