| I don't like that at all. Sure if you consider: for (auto& p : wordCount) { // ... word: p.first, count: p.second
}then for (auto& [word, count] : wordCount) { // C++1z // ...
}seems like an improvement because you immediately know the semantics of the two parts of the pair. But you have no idea what "word" or "count" is. In this example with words like "word" or "count" the semantics somehow encode type information but most of the time you deal with user defined types where that information is a) not clear and b) not as trivial. To me this whole "auto" thing seems misunderstood. People use it to make their lifes "easier" (think lazy) but not simpler. The type system is there to help you and type information is of uttermost importance and should be available as closest to the usage as possible without destroying readability so you don't have to look it up somewhere else. using wcPair = std::pair<const std::string, int>; for (wcPair& p : wcMap) { std::cout << p.first << " : " << p.second << std::endl;
}seems reasonable enough.
The loop itself can be read without clutter, you can infer what's supposed to happen without knowing much stuff and even if you want a deeper understanding of what's going on then the information you need is right above. Doesn't that seem much better? oO ...and if you want a language without a type system don't use a language with a type system. |
I wasn't the one who downvoted you but I'm guessing it happened because your advice is misguided and it shows how you're confusing the ceremony of a human bashing extra keystrokes to repeat annotations everywhere with the concept of static typing that enables compiler correctness checking. The keyword "auto" enables the separation of those 2 concepts. Type inferencing and type deduction is the technology that gives you "types" without the tedious "ceremony".