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
4 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
May 4, 2020 at 20:10 | comment | added | Seva Alekseyev | There's one extremely popular use case: ref counts. | |
Jan 7, 2019 at 16:21 | comment | added | Th. Thielemann |
The usage of mutable can make the code more readable and cleaner. In the following example, the read can be const as expected. ` mutable m_mutex; Container m_container; void add(Item item) { Lockguard lock(m_mutex); m_container.pushback(item); } Item read() const { Lockguard lock(m_mutex); return m_container.first(); } `
|
|
Jul 25, 2012 at 14:08 | comment | added | enthusiasticgeek | I think this link gives the best example of a scenario where mutable is helpful.It almost appears they are exclusively used for debugging. (per correct usage) | |
Sep 19, 2008 at 20:02 | history | answered | John Millikin | CC BY-SA 2.5 |