What are the pros/cons to using the auto
keyword, especially in for loops?
for(std::vector<T>::iterator it = x.begin(); it != x.end(); it++ )
{
it->something();
}
for(std::map<T>::iterator it = x.begin(); it != x.end(); it++ )
{
it->second->something();
}
for(auto it = x.begin(); it != x.end(); it++ )
{
it->??
}
Seems like if you don't know whether you have an iterator for a map or a vector you wouldn't know whether to use first
or second
or just directly access properties of the object, no?
This reminds me of the C# debate on whether to use the keyword var
. The impression I'm getting so far is that in the C++ world people are ready to adopt the auto
keyword with less of a fight than var
in the C# world. For me my first instinct is that I like to know the type of the variable so I can know what operations I can expect to perform on it.
var
? I missed that.for (auto& it : x)
(or without reference if you want copying)x
and you don't even know whatx
is, you shouldn't write that loop in the first place ;-)