What is the correct use of auto?

Robert Fraser fraserofthenight at gmail.com
Fri Apr 11 15:05:15 PDT 2008


Bill Baxter wrote:
> Unknown W. Brackets wrote:
>> It might help you to understand auto if you try this:
>>
>> auto i;
>>
>> It will not compile.  In this case, i has no type.  Remember that i is 
>> not some boxed variable or something... it's simply type inference.
>>
>> -[Unknown]
> 
> And also you should be aware that it's the *lack* of a specific type 
> that triggers the inference, not the *presence* of auto.  These do type 
> inference too:
> 
> // D1
> static x = 2.3;
> static assert(is(typeof(x)==double));
> 
> const y = "hello";
> static assert(is(typeof(y)==char[]));
> 
> --bb

In fact, if you say "auto static x = new object()" the "auto" has a 
different meaning (the same meaning as "scope"). This part, of course, 
is inane and confusing but good for backwards compatibility.



More information about the Digitalmars-d mailing list