Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes it ever make sense to make a fundamental (non-pointer) parameter const?
    text
    copied!<p>I recently had an exchange with another C++ developer about the following use of <code>const</code>:</p> <pre><code>void Foo(const int bar); </code></pre> <p>He felt that using <code>const</code> in this way was good practice.</p> <p>I argued that it does nothing for the caller of the function (since a copy of the argument was going to be passed, there is no additional guarantee of safety with regard to overwrite). In addition, doing this prevents the implementer of <code>Foo</code> from modifying their private copy of the argument. So, it both mandates and advertises an implementation detail.</p> <p>Not the end of the world, but certainly not something to be recommended as <em>good practice</em>.</p> <p>I'm curious as to what others think on this issue.</p> <h3>Edit:</h3> <p>OK, I didn't realize that const-ness of the arguments didn't factor into the signature of the function. So, it is possible to mark the arguments as <code>const</code> in the implementation (.cpp), and not in the header (.h) - and the compiler is fine with that. That being the case, I guess the policy should be the same for making local variables const. </p> <p>One could make the argument that having different <em>looking</em> signatures in the header and source file would confuse others (as it would have confused me). While I try to follow the <a href="http://en.wikipedia.org/wiki/Principle_of_least_astonishment" rel="noreferrer">Principle of Least Astonishment</a> with whatever I write, I guess it's reasonable to expect developers to recognize this as legal and useful.</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