auto -> auto & var

Chris Nicholson-Sauls ibisbasenji at gmail.com
Mon Mar 13 10:35:12 PST 2006


Lionello Lunesu wrote:
> "Kramer" <Kramer_member at pathlink.com> wrote in message 
> news:duspas$2dfo$1 at digitaldaemon.com...
> 
>>I keep tripping myself over the double meaning of auto.  I know it 
>>shouldn't be
>>that difficult to remember that auto means type inference and also stack
>>allocation/RAII, but does it have to be that way?
> 
> 
> Walter explained it: it does not have two meanings. The "auto" merely 
> notifies the compiler (or lexer, I don't know) that it's a declaration and a 
> variable name will follow.
> 
> When the compiler encounters "auto x" it knows "x" is a variable. Since 
> "auto x" doesn't mention a type, it'll be derived.. Same thing for "static 
> x", x must be a variable, but there's no type so it'll be derived.
> 
> L.

Actually there is a difference.  If I use `static x = new Foo;` then 'x' is static.  If I 
use `const x = "abc"c;` then 'x' is const.  But if I use `auto x = new Foo;` then 'x' is 
NOT auto.  For whatever reason, the auto attribute, when used to trigger type inference, 
does not continue to apply to the variable, whereas the other attributes with this 
behavior do continue to apply.

Although for the record, I'm mostly fine with using auto for this, anyhow.  I just wonder 
how to declare an inferred variable that is also auto.  Is it `auto auto x =...`)

-- Christopher Nicholson-Sauls



More information about the Digitalmars-d mailing list