auto storage class - infer or RAII?

Bill Baxter wbaxter at gmail.com
Sun Nov 12 13:38:31 PST 2006


Walter Bright wrote:
> Bill Baxter wrote:
> 
>> Which is good, since automatic type deduction is basically a typing 
>> saving feature to begin with.
> 
> 
> No, it's way way more than that. Auto type deduction gets us much closer 
> to the goal of the type of something only needs to be specified once, 
> and then anything that manipulates it infers the right type. This makes 
> code much more robust.
> 
> For example,
> 
>     auto len = foo.length;
> 
> What is the type of foo.length? Is it an int? uint? size_t? class Abc? 
> Without type inference, I'd have to go look it up. With type inference, 
> I know I've got the *correct* type for i automatically.

 From a software maintenance and robustness standpoint I think that cuts 
both ways.  If I don't know the type then it's easy to do something 
wrong with it like

    auto len = foo.length;
    len = -1;  // oops! len is unsigned!  not what I intended!

 From the point of view of someone reading and maintaining code, I hope 
the only places I see auto are where the type is obvious from the 
context, like auto foo = new SomeClassWithALongNameIDontWantToTypeTwice.

I think it's bad practice to use auto just becuase you're too lazy to go 
figure out what the right type is.  If you don't figure it out when 
writing it, then every person after you reading or maintaining the code 
is going to have to go figure it out.

--bb



More information about the Digitalmars-d mailing list