Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Grouping variable declarations at the top of the block is a legacy likely due to limitations of old, primitive C compilers. All modern languages recommend and sometimes even enforce the declaration of local variables at the latest point: where they're first initialized. Because this gets rid of the risk of using a random value by mistake. Separating declaration and initialization also prevents you from using "const" (or "final") when you could.</p> <p>C++ unfortunately keeps accepting the old, top declaration way for backward compatibility with C (one C compatibility drag out of many others...) But C++ tries to move away from it:</p> <ul> <li>The design of C++ references does not even allow such top of the block grouping.</li> <li>If you separate declaration and initialization of a C++ local <em>object</em> then you pay the cost of an extra constructor for nothing. If the no-arg constructor does not exist then again you are not even allowed to separate both!</li> </ul> <p>C99 starts to move C in this same direction.</p> <p>If you are worried of not finding where local variables are declared then it means you have a much bigger problem: the enclosing block is too long and should be split.</p> <p><a href="https://www.securecoding.cert.org/confluence/display/cplusplus/DCL19-CPP.+Initialize+automatic+local+variables+on+declaration" rel="noreferrer">https://www.securecoding.cert.org/confluence/display/cplusplus/DCL19-CPP.+Initialize+automatic+local+variables+on+declaration</a></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