I'm currently updating some older C++ code that I have that iterates over STL container types, and I'm finding a lot of code like this:
if (!edgeSet.empty())
{
for(typename EdgeSet::const_iterator iter = edgeSet.begin(); iter != edgeSet.end(); iter++)
{
...
}
}
Which I am turning (successfully) into the equivalent:
if (!edgeSet.empty())
{
for(auto& edge : edgeSet)
{
...
}
}
And I was wondering... is the "empty" check necessary for a "range for"? I would imagine it would be necessary in the older "iterator style" for loop to avoid a useless initialization and comparison / branch, but I'm curious if a "range for" automatically does an empty check or not before it even starts.
Further, if the optimization does happen, does it happen at all optimization levels (including no optimization: -O0 )?
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire