auto storage class - infer or RAII?

Bill Baxter dnewsgroup at billbaxter.com
Sun Nov 12 16:14:42 PST 2006


Walter Bright wrote:
> Bill Baxter wrote:
>>  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!
> 
> If you wrote:
> 
>     int len = foo.length;
> 
> and foo.length was unsigned, isn't that just another sort of bug?

Yeh, I suppose.  On the other hand my specification of a type is a kind 
of contract that the compiler can check.  Both have their uses, but I'm 
just saying specifying the type should be the default policy, unless 
there's a compelling reason not to (i.e. it's obvious from context).

I guess the best maintenance argument for why auto should be used 
sparingly is that information should be as localized as possible.  It's 
why C++ gurus say you should declare at point of use whenever possible 
(rather than using C-style top-of-the-function declarations).  It's also 
one reason why globals are considered bad, and why creative operator 
overloading is a bad idea.  All those things can make you have to go 
searching all over your codebase to figure out the meaning of one little 
line of code.

--bb



More information about the Digitalmars-d mailing list