I've been using the new auto
keyword available in the C++11 standard for complicated templated types which is what I believe it was designed for. But I'm also using it for things like:
auto foo = std::make_shared<Foo>();
And more skeptically for:
auto foo = bla(); // where bla() return a shared_ptr<Foo>
I haven't seen much discussion on this topic. It seems that auto
could be overused since a type is often a form of documentation and sanity checks. Where do you draw the line in using auto
and what are the recommended use cases for this new feature?
To clarify: I'm not asking for a philosophical opinion; I'm asking for the intended use of this keyword by the standard committee, possibly with comments on how that intended use is realized in practice.
Best Answer
I think that one should use the
auto
keyword whenever it's hard to say how to write the type at first sight, but the type of the right hand side of an expression is obvious. For example, using:to get the composite key type in
boost::multi_index
, even though you know that it isint
. You can't just writeint
because it could be changed in the future. I would writeauto
in this case.So if the
auto
keyword improves readability in a particular case then use it. You can writeauto
when it is obvious to the reader what typeauto
represents.Here are some examples: