Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>The only one I can think of, is so that you don't mess up and assign to it accidentally?</p> </blockquote> <p>Yes, that's the point of <code>const</code>. Because of that, it's easier to analyse and reason about the purpose and correctness of the function.</p> <p>Still, it's generally considered bad practice to make by-value (i.e. non-pointer/non-reference) arguments in public APIs <code>const</code>: if you later want to modify them in the implementation you'll need to choose between: - edit the public header to remove <code>const</code>, which can trigger recompilation of client code (as is common with file modification timestamp driven <code>make</code> rules), - if you don't remove the <code>const</code> from the implementation you may be forced to make an inefficient copy to yet another variable just to be able to modify the value.... - allow the declaration and definition to differ, which can confuse programmers flicking between the two (if they remember seeing it const somewhere but that isn't the implementation, they may make assumptions that prove to be wrong - the same danger doesn't exist for a non-<code>const</code> declaration that is actually <code>const</code> - at worst they check things unnecessarily carefully to find out the current state of the variable).</p> <p>So, for functions internal to an implementation file, use <code>const</code> if you think it adds value (sometimes the verbosity is enough to make you not bother), but actively avoid it in public header files.</p> <p>In Exceptional C++, Herb Sutter recommends:</p> <blockquote> <p>"Avoid const pass-by-value parameters in function declarations. Still make the parameter const in the same function's definition if it won't be modified."</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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