Skip to content
lordloki edited this page Sep 15, 2018 · 1 revision

The C++11 autokeyword 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::vectordoesn't request to use auto as it needs to only write the item type.

Clone this wiki locally