Does a mutable member of an object const const?

In C ++, you can now have members mutable. This adds a “logical const” layer to the language. How do they relate to read-only data - will it have an element mutableto prevent the const class from being placed in the section .rodata?

class Foo {
    mutable int bar;

public:
    Foo(): bar(0) {}
    void set(int x) const { bar = x; }
};

// Can this be in a read-only section?
const Foo foo;

int main(void)
{
    // Is this well-defined?
    foo.set(5);
}
+4
source share
2 answers

Yes, you are allowed to change the mutable members of const objects, this is described in the draft C ++ project 7.1.1 Specification of the storage class which states:

, , const (7.1.6.1).

++ 7.1 , . , :

, ROMability , , .

+5

mutable const , , ROMability. ROMable. " Techincal ++ Perfomance" 7

+2

All Articles