Is C 11's Auto Keyword Overkill?
While the auto keyword has proven invaluable in handling complex templated types, as intended by its designers, concerns arise over its overuse in simpler scenarios. Should auto be used to avoid explicitly stating types, even for straightforward expressions like std::make_shared
Intended Use and Practical Guidelines
According to the C standard committee, auto should be employed when explicitly stating the type is challenging but the type of the right-hand side is evident. For example, consider the following code:
my_multi_type::nth_index<2>::type::key_type::composite_key_type:: key_extractor_tuple::tail_type::head_type::result_type
Retrieving the composite key type in boost::multi_index requires this complex expression. Even though the type is known to be int, auto should be used to enhance readability in such cases.
Recommended Use Cases
Use auto when:
Examples
Cautionary Tales
The above is the detailed content of Is C 11's `auto` Keyword Overused: When Should You Avoid Explicit Typing?. For more information, please follow other related articles on the PHP Chinese website!