Stefan Ram <
r...@zedat.fu-berlin.de> wrote:
> I just read a web page »Almost Always Auto« and wanted
> to give it a try.
I'd suggest the exact opposite principle: Almost never auto.
Strong typing is one of the advantages of C++, as it catches mistakes
at compile time (which is one of the best stages at which to catch
mistakes). If you have something like:
MyType result = foo();
and it turns out that foo() doesn't actually return a value that's
compatible with MyType, the compiler will immediately tell you. If you
used auto, the error message would much more obscure, and in a few cases
it could even compile but malfunction, which would be the worst possible
scenario (and something that's avoided by not using 'auto').
That's not to say that 'auto' is useless. On the contrary. It's extremely
useful. It's very useful in generic code, especially templated code.
It's also useful in non-generic code, but should be used there only when
it makes sense to use it. In general, in non-generic code it should/can be
used when it really is so that the actual type doesn't matter. This may be
in cases where the code just doesn't care what the type is, or when there
is some guarantee about the behavior of the type, but the exact name of the
type is irrelevant (iterators are a perfect example.)
--- news://
freenews.netfront.net/ - complaints:
ne...@netfront.net ---