David Brown <
david...@hesbynett.no> wrote:
> Replacing the return type entirely with auto has the same pros and cons
> as using auto in general. It is convenient for complicated types, but
> omits information that might be useful to the programmer and reader.
That depends.
The 'auto' variable declaration type is most useful in generic code,
especially templates and functions with 'auto' parameters.
"What, you mean there's other kind of 'generic code' than templates?"
Well, kind of. There are certain situations where it's actually useful
that 'auto' adapts to any changes to a type. The kind of situations where
if you change a type somewhere, then you would need to go through thousands
of lines of code to change the usage of that type. Of course traditionally
this has been circumvented by abstracting the type away with typedef
(which is still a completely valid technique, of course), but in some
situations 'auto' may be a more fluent solution.
In the same vein, there may be situations where we don't actually care
what a type is exactly, because we are not interested in it. A typical
example is something like:
auto x = std::bind(&some_function, _2, _1, some_object);
Here we don't actually care what the type returned by std::bind() is.
We only care about what it does.
And then of course there are situations where 'auto' shortens the code
and makes it cleaner without sacrificing undestandability. The typical
example is:
auto iter = container.begin();
The type of that iterator may sometimes be very long and superfluous.
'auto' is perfect in this situation, and is quite clear what it does.
Of course if abused, like anything else, it can lead to bad code.
You should always know your tools.