-
-
Notifications
You must be signed in to change notification settings - Fork 183
Auto usage
lordloki edited this page Sep 15, 2018
·
1 revision
The C++11 auto
keyword is allowed only for complicated iterator types coming from the standard template library.
For example:
std::map<int, int> map;
…
for (const auto& pair : map) {
// Warning: the "pair" is a std::pair not a std::map<int, int>::iterator.
pair.second.doWork();
}
Instead of:
std::map<int, int> map;
…
for (std::map<int, int>::const_iterator it = map.begin(), end = map.end(); it != end; ++it) {
it->second.doWork();
}
Other type like std::vector
doesn't request to use auto as it needs to only write the item type.