Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Let's talk about the change in <code>const</code>.</p> <pre><code>void somefunc(Foo&amp;); void somefunc(const Foo&amp;); </code></pre> <p>In C++03 and before, the <code>const</code> version, compared to the non-<code>const</code> one, provides additional guarantees to the callers. It promises not to modify its argument, where by modification we mean calling <code>Foo</code>'s non-const member functions (including assignment etc), or passing it to functions that expect a non-<code>const</code> argument, or doing same to its exposed non-mutable data members. <code>somefunc</code> restricts itself to <code>const</code> operations on <code>Foo</code>. And the additional guarantee is totally one-sided. Neither the caller nor the <code>Foo</code> provider do not have to do anything special in order to call the <code>const</code> version. Anyone who is able to call the non-<code>const</code> version can call the <code>const</code> version too.</p> <p>In C++11 this changes. The <code>const</code> version still provides the same guarantee to the caller, but now it comes with a price. <em>The provider of <code>Foo</code> must make sure that all <code>const</code> operations are thread safe</em>. Or it least it must do so when <code>somefunc</code> is a standard library function. Why? Because the standard library <em>may</em> parallelize its operations, and it <em>will</em> call <code>const</code> operations on anything and everything without any additional synchronization. So you, the user, must make sure this additional synchronization is not needed. Of course this is not a problem in most cases, as most classes have no mutable members and most <code>const</code> operations don't touch global data.</p> <p>So what <code>mutable</code> means now? It's the same as before! Namely, this data is non-const, but it is an implementation detail, I promise it does not affect the observable behavior. This means that no, you don't have to mark everything in sight <code>mutable</code>, just as you didn't do it in C++98. So when you should mark a data member <code>mutable</code>? Just like in C++98, when you need to call its non-<code>const</code> operations from a <code>const</code> method, and you can guarantee it won't break anything. To reiterate:</p> <ul> <li>if your data member's physical state does not affect the observable state of the object</li> <li><em>and</em> it is thread-safe (internally synchronized)</li> <li>then you can (if you need to!) go ahead and declare it <code>mutable</code>.</li> </ul> <p>The first condition is imposed, like in C++98, because other code, including the standard library, may call your <code>const</code> methods and nobody should observe any changes resulting from such calls. The second condition is there, and this is what's new in C++11, because such calls can be made asynchronously.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload