Why auto is bad in c++
When auto keyword is available in c++ 11, it becomes a modern c++ standard. Developer feels elegant to use it, but this elegant keyword is risky to your program.
When we use auto, that means we are not taking control of the variable type. The Right Hand Side (RHS) controls it.
The type of sizeOfVar
is controlled by the sizeof()
and the return type of sizeof()
is dependent on the compiler. sizeOfVar may be unsigned long or unsigned long long and you have no idea on your code.
The most common way of using auto keyword is in the for loop. We use auto (4 characters) to replace int (3 characters).
The above code is fine. But if we reverse the iteration, the program will crash.
size()
returns a size_t type (unsigned int) which is never below zero. And so
(unsigned int) 0–1 = 4294967295
The i >= 0 condition will always be satisfied and the program will throw an out of range error in line 3. auto should be used very carefully. However, if we know it is an int type, why should we type one more character and get ourselves uncomfortable.
However, auto sometimes is useful if the type name is significantly long, for instance:
std::chrono::system_clock::time_point tim = getTime();
Even through we can see the type name on your code, you still have no clue what it is, right?
Enjoy Reading This Article?
Here are some more articles you might like to read next: