patterncppCritical
How much is too much with C++11 auto keyword?
Viewed 0 times
autowithhowtoomuchkeyword
Problem
I've been using the new
And more skeptically for:
I haven't seen much discussion on this topic. It seems that
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.
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();And more skeptically for:
auto foo = bla(); // where bla() return a shared_ptrI 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.
Solution
I think that one should use the
to get the composite key type in
So if the
Here are some examples:
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:my_multi_type::nth_index::type::key_type::composite_key_type::
key_extractor_tuple::tail_type::head_type::result_typeto get the composite key type in
boost::multi_index, even though you know that it is int. You can't just write int because it could be changed in the future. I would write auto in this case.So if the
auto keyword improves readability in a particular case then use it. You can write auto when it is obvious to the reader what type auto represents.Here are some examples:
auto foo = std::make_shared(); // obvious
auto foo = bla(); // unclear. don't know which type `foo` has
const size_t max_size = 100;
for ( auto x = max_size; x > 0; --x ) // unclear. could lead to the errors
// since max_size is unsigned
std::vector v;
for ( auto it = v.begin(); it != v.end(); ++it )
// ok, since I know that `it` has an iterator type
// (don't really care which one in this context)Code Snippets
my_multi_type::nth_index<2>::type::key_type::composite_key_type::
key_extractor_tuple::tail_type::head_type::result_typeauto foo = std::make_shared<Foo>(); // obvious
auto foo = bla(); // unclear. don't know which type `foo` has
const size_t max_size = 100;
for ( auto x = max_size; x > 0; --x ) // unclear. could lead to the errors
// since max_size is unsigned
std::vector<some_class> v;
for ( auto it = v.begin(); it != v.end(); ++it )
// ok, since I know that `it` has an iterator type
// (don't really care which one in this context)Context
Stack Overflow Q#6434971, score: 166
Revisions (0)
No revisions yet.