It seems that auto
was a fairly significant feature to be added in C++11 that seems to follow a lot of the newer languages. As with a language like Python, I have not seen any explicit variable declaration (I am not sure if it is possible using Python standards).
Is there a drawback to using auto
to declare variables instead of explicitly declaring them?
auto
does not have drawbacks per se, and I advocate to (hand-wavily) use it everywhere in new code. It allows your code to consistently type-check, and consistently avoid silent slicing. (IfB
derives fromA
and a function returningA
suddenly returnsB
, thenauto
behaves as expected to store its return value)Although, pre-C++11 legacy code may rely on implicit conversions induced by the use of explicitly-typed variables. Changing an explicitly-typed variable to
auto
might change code behaviour, so you'd better be cautious.