The auto keyword should be used mainly in cases where you initialize a variable with a value, since this makes the code more convenient to maintain in cases where you change the value that you initialize a variable with:
uint16_t id_ = 65535; uint16_t id() { return id_; } auto myid = id();
It is not necessary to change the type myid if id() change the type of the return value.
And with C ++ 14 it gets even better:
uint16_t id_ = 65535; decltype(auto) id() { return id_; } auto myid = id();
Changing the id_ type automatically configures the return type id() and type myid .
In cases where the initial value of the variable is not initialized by the value and, as such, does not depend on other code for initialization, it makes sense to explicitly determine the type of the variable, since the auto keyword will not add more maintainability to the code and syntax auto gender = string{}; less readable than string gender; .
source share