DEV Community

Discussion on: The awesome `auto` keyword

Collapse
 
pauljlucas profile image
Paul J. Lucas • Edited

auto i = 0;

Except that doesn't always mean what you think it means. If you originally did:

unsigned i = 0;
Enter fullscreen mode Exit fullscreen mode

then the type of i is unsigned (obviously); but if you change that to auto as above, then the type of i becomes int because 0 is an int literal. If you want it to stay unsigned, then you have to be explicit:

auto i = 0u;
Enter fullscreen mode Exit fullscreen mode

You have the same problem for short, long, long long, and their unsigned variants.

for (std::vector<int> row : matrix) {

This is incorrect. In the "older standards" (C++03 and earlier), range-based for loops weren't a thing yet. (Even if they were, it's still incorrect since you're missing ::const_iterator.) Range-based for loops were addd to C++ at the same time as auto in C++11, so your "older standards" code should have been:

for (std::vector<int>::const_itererator i = maxtrix.begin(); i != matrix.end(); ++i ) {
Enter fullscreen mode Exit fullscreen mode