Question Details

No question body available.

Tags

c++ stdmap c++23 range-based-loop

Answers (2)

Accepted Answer Available
Accepted Answer
December 15, 2025 Score: 14 Rep: 65,636 Quality: Expert Completeness: 60%

auto && will match the const/volatile/ref state of each variable.

for (auto &&[k, v] : m) {
    v++;
}

See it working

December 15, 2025 Score: 22 Rep: 124,598 Quality: High Completeness: 50%

The reference type in std::flatmap is std::pair which is different from that in std::map / std::unorderedmap where it is std::pair&. Note that the references are in the pair in a flatmap. So, you need to take k and v by value:

for (auto [k, v] : m) {
    v++;
}

Both k and v are here references to the values inside your flatmap.

The reason for this is simply that a flat_map does not store std::pairs internally. It has two separate SequenceContainer s for keys and values and creates the pair when you dereference the iterator.