Timeline for Does the 'mutable' keyword have any purpose other than allowing a data member to be modified by a const member function?
Current License: CC BY-SA 2.5
5 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jun 18, 2017 at 22:32 | comment | added | BeeOnRope |
Now, some compilers have special extensions such as gcc's _attribute__((const)) and __attribute__((pure)), which _do have such effects, but that's only tangentially related to the const keyword in C++.
|
|
Jun 18, 2017 at 22:28 | comment | added | BeeOnRope |
@HagenvonEitzen - I'm pretty sure that is incorrect. A compiler cannot hoist functions out of a loop unless it can prove there are no side effects. That proof generally involves actually inspecting the implementation of the function (often after it is inlined) and not relying on const (and such an inspection will succeed or fail irrespective of const or mutable ). Simply declaring the function const isn't enough: a const function is free to have side effects such as modifying a global variable or something passed to the function, so it isn't a useful guarantee for that proof.
|
|
Aug 19, 2014 at 12:31 | comment | added | Hagen von Eitzen | Apart from placing a const object in read-only memory, the compiler may also decide to optimize const fucntion calls out of a loop, for example. A mutable statistics counter in an otherwise const function will still allow such optimization (and count only one call) instead of preventing optimization just for the sake of counting more calls. | |
Sep 22, 2008 at 8:17 | comment | added | Richard Corden | Good answer, except for comment regarding mutable being a "hint". This makes it seem as if the mutable member sometimes won't be mutable if the compiler placed the object into ROM. The behaviour of mutable is well defined. | |
Sep 19, 2008 at 20:25 | history | answered | Lloyd | CC BY-SA 2.5 |