c++ - How much is too much with C++11 auto keyword? -
i've been using new auto
keyword available in c++11 standard complicated templated types believe designed for. i'm using things like:
auto foo = std::make_shared<foo>();
and more skeptically for:
auto foo = bla(); // bla() return shared_ptr<foo>
i haven't seen discussion on topic. seems auto
overused, since type form of documentation , sanity checks. draw line in using auto
, recommended use cases new feature?
to clarify: i'm not asking philosophical opinion; i'm asking intended use of keyword standard committee, possibly comments on how intended use realized in practice.
side note: question moved se.programmers , stack overflow. discussion can found in meta question.
i think 1 should use auto
keyword whenever it's hard how write type @ first sight, type of right hand side of expression obvious. example, using:
my_multi_type::nth_index<2>::type::key_type::composite_key_type::\ key_extractor_tuple::tail_type::head_type::result_type
to composite key type in boost::multi_index
, though know int
. can't write int
because changed in future. write auto
in case.
so if auto
keyword improves readability in particular case use it. can write auto
when obvious reader type auto
represents.
here examples:
auto foo = std::make_shared<foo>(); // obvious auto foo = bla(); // unclear. don't know type `foo` has const size_t max_size = 100; ( auto x = max_size; x > 0; --x ) // unclear. lead errors // since max_size unsigned std::vector<some_class> v; ( auto = v.begin(); != v.end(); ++it ) // ok, since know // `it` has iterator type (don't care 1 in context)
Comments
Post a Comment