Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>auto</code> is a storage class specifier, <code>static</code>, <code>register</code> and <code>extern</code> too. You can only use one of these four in a declaration. </p> <p>Local variables (without <code>static</code>) have automatic storage duration, which means they live from the start of their definition until the end of their block. Putting auto in front of them is redundant since that is the default anyway. </p> <p>I don't know of any reason to use it in C++. In old C versions that have the implicit int rule, you could use it to declare a variable</p> <pre><code>int main(void) { auto i = 1; } </code></pre> <p>To to make it valid syntax or disambiguate from an assignment expression in case <code>i</code> is in scope. But this doesn't work in C++ anyway (you have to specify a type). Funny enough, the C++ Standard writes:</p> <blockquote> <p>An object declared without a storage-class-specifier at block scope or declared as a function parameter has automatic storage duration by default. [Note: hence, the auto specifier is almost always redundant and not often used; one use of auto is to distinguish a declaration-statement from an expression-statement (6.8) explicitly. —end note]</p> </blockquote> <p>Which refers to the following scenario, which could be either a cast of <code>a</code> to int or the declaration of a variable <code>a</code> of type <code>int</code> having redundant parentheses around <code>a</code>. It is always taken to be a declaration, so <code>auto</code> wouldn't add anything useful here, but would for the human, instead. But then again, the human would be better off removing the redundant parentheses around <code>a</code>, I would say.</p> <pre><code>int(a); </code></pre> <p>With the new meaning of <code>auto</code> arriving with C++0x, I would discourage using it with C++03's meaning in code. </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