Mr Flibble <
flibbleREM...@i42.co.uk> wrote:
> C++ has also
> introduced the 'auto' keyword which hides the original type.
Using 'auto' can be a double-edged sword.
When you explicitly specify the type, it catches mistakes more easily.
For example, if you write:
std::vector<Type> vec = someFunction();
you are explicitly expressing that you expecting that function to
return a vector of that type. If you make a mistake and the function
returns something else, the error message will be immediate and clear.
However,
auto vec = someFunction();
is less readable because it doesn't express what you are expecting
that return value type to be, and if you make a mistake, the error
will happen later in the code, and can sometimes be very obscure.
In a few instances the program might even compile, and then
malfunction at runtime.
That being said, in generic code 'auto' can be very useful.
Perhaps also in non-generic code where 'auto' is used as a shortcut
to substitute a very long type, and it's very clear from the context
what that type is supposed to be.