Timeline for Does auto make C++ code harder to understand?
Current License: CC BY-SA 3.0
14 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Oct 5, 2018 at 8:41 | comment | added | Caleth |
@stijn no, it's totally fine to use begin() and end() . The intention is "give me this range", which is what you get. Now that we have std::as_const we don't need cbegin etc.
|
|
Dec 9, 2017 at 15:48 | comment | added | underscore_d |
then the next point is that the code would be made even more generic by using the non-member std::cbegin|end() , as those can work for plain arrays too, which is especially useful when writing templates, but could equally work if you changed the argument to a reference-to-array type. (then there's a more general argument to prefer non-members wherever possible, though I don't really grasp all the angles of that one)
|
|
Jan 15, 2013 at 20:46 | comment | added | stijn | imo the example should use cbegin() and cend() as it's (sort of) more const-correct and because it clarifies the intent better especially in the light of auto: it tells 'hey I'm a const iterator' and you don't even have to look at the definition of x to know it is. | |
Dec 21, 2012 at 22:51 | comment | added | David Thornley | The other interesting thing about that loop is that it works for any x with forward iterators. It doesn't matter if x is an array, a list, a map, whatever. If all you've decided at this point is that x will be a container containing a specific type of object, you can just write this and not come back to it. | |
Dec 20, 2012 at 22:47 | comment | added | Martin Beckett |
I've only started writing iterator loops since auto .
|
|
Dec 20, 2012 at 21:25 | history | migrated | from stackoverflow.com (revisions) | ||
Dec 20, 2012 at 17:05 | comment | added | MSalters |
TBH I'd prefer for (anX : x) to make it even more obvious we're just iterating over x . The normal case where you need an iterator is when you're modifying the container, but x is const&
|
|
Dec 20, 2012 at 14:33 | comment | added | R. Martinho Fernandes |
Also note that having a properly named x would make knowing the type here even less interesting. At the end of the day, I think that responsible use of auto leads to much better names and thus more readable code.
|
|
Dec 20, 2012 at 14:31 | comment | added | Steve Jessop |
@Nawaz: and that you can't assign to it->second since it's a const iterator. All of which information is a repeat of what's in the previous line, const std::map<int, std::string>& x . Saying things multiple times does occasionally inform better, but by no means is that a general rule :-)
|
|
Dec 20, 2012 at 14:27 | vote | accept | Mircea Ispas | ||
Nov 26, 2015 at 8:56 | |||||
Dec 20, 2012 at 14:27 | comment | added | Sarfaraz Nawaz |
@SteveJessop: It tells me two things at least : the key is int , and the value is std::string . :)
|
|
Dec 20, 2012 at 14:25 | comment | added | Steve Jessop |
+1. Even if you did name the type, you'd name it as std::map<int, std::string>::const_iterator , so it's not as if the name tells you much about the type anyway.
|
|
Dec 20, 2012 at 14:05 | comment | added | R. Martinho Fernandes | Exactly. Who the heck cares about the type. It's an iterator. I don't care about the type, all I need to know is that I can use it to iterate. | |
Dec 20, 2012 at 14:03 | history | answered | Luchian Grigore | CC BY-SA 3.0 |