I can see why the auto
type in C++11 improves correctness and maintainability. I've read that it can also improve performance (Almost Always Auto by Herb Sutter), but this part lacks a good explanation.
- How can
auto
improve performance? - Can anyone give an example?
Best Answer
auto
can aid performance by avoiding silent implicit conversions. An example I find compelling is the following.See the bug? Here we are, thinking we're elegantly taking every item in the map by const reference and using the new range-for expression to make our intent clear, but actually we're copying every element. This is because
std::map<Key, Val>::value_type
isstd::pair<const Key, Val>
, notstd::pair<Key, Val>
. Thus, when we (implicitly) have:Instead of taking a reference to an existing object and leaving it at that, we have to do a type conversion. You are allowed to take a const reference to an object (or temporary) of a different type as long as there is an implicit conversion available, e.g.:
The type conversion is an allowed implicit conversion for the same reason you can convert a
const Key
to aKey
, but we have to construct a temporary of the new type in order to allow for that. Thus, effectively our loop does:(Of course, there isn't actually a
__tmp
object, it's just there for illustration, in reality the unnamed temporary is just bound toitem
for its lifetime).Just changing to:
just saved us a ton of copies - now the referenced type matches the initializer type, so no temporary or conversion is necessary, we can just do a direct reference.