skip to content

In C++, why does `for (const std::pair<std::string, Order>& e : book)` over a `std::map` copy every entry, and how does `auto` fix it?

level: middleimportance: must knowfreq 62%

answer

  1. value_type has a const key
  2. different pair type, temporary built
  3. -Wrange-loop-construct
  4. const auto& binds the element itself

basics

~20 s

A map's elements are std::pair<const std::string, Order>; the loop names a pair without const, so each iteration builds and binds a temporary copy. const auto& deduces the exact element type and binds to the entry itself.

solid answer

~30 s

`std::map<std::string, Order>::value_type` is `std::pair<const std::string, Order>`, because keys must not change in place. The hand-written loop variable names `std::pair<std::string, Order>`, a different type, so the `const&` cannot bind to the element; it binds to a temporary that `std::pair`'s converting constructor builds from it, copying the key and the `Order` on every iteration. Clang and GCC flag this with `-Wrange-loop-construct` under `-Wall`. `const auto& e` deduces `const std::pair<const std::string, Order>&` and binds to the element; `auto& e` allows writing `e.second`; `const auto& [id, order]` names the members of the bound element without copying, and `id` keeps the key's `const`.

code

cpp · 18 lines
cpp
#include <cstdio>
#include <map>
#include <string>

struct Order {
    double px = 0;
    Order(double p) : px(p) {}
    Order(const Order& o) : px(o.px) { std::puts("copy Order"); }
};

int main() {
    std::map<std::string, Order> book;
    book.emplace("A1", 101.5);
    for (const std::pair<std::string, Order>& e : book)   // value_type has a const key: temporary
        std::printf("%s %.1f\n", e.first.c_str(), e.second.px);
    for (const auto& [id, order] : book)                   // binds to the element itself
        std::printf("%s %.1f\n", id.c_str(), order.px);
}

go deeper

for a junior

Recall that map elements are pair<const Key, T>, and that const auto& is the safe way to iterate them.

for a middle

Explain why a const reference to a different pair type binds to a converted temporary, and what each auto form deduces in a range-for.

for a senior

Find hidden per-iteration copies in hot loops with -Wrange-loop-construct, and explain structured bindings as names for members of one hidden variable.

for a principal

Make range-loop copy warnings errors in latency-sensitive code and standardise const auto& for read-only iteration over containers.

## The element type of a map is not what people write For `std::map<Key, T>` and `std::unordered_map<Key, T>`, the element type, `value_type`, is `std::pair<const Key, T>`. The key is `const` because changing it in place would break the container's ordering or hashing. Iterating yields references to that exact type: `std::pair<const std::string, Order>&`. Now look at a loop that spells the type by hand: ```cpp for (const std::pair<std::string, Order>& e : book) { ... } ``` `std::pair<std::string, Order>` and `std::pair<const std::string, Order>` are **different types**. A `const` reference to one cannot bind directly to the other, but it can bind to a **temporary**: `std::pair` has a converting constructor, so on every iteration the compiler constructs a new `std::pair<std::string, Order>` from the element, copying the key string and the `Order`, binds `e` to it, and destroys it at the end of the iteration. The loop works, prints the right values, and is silently doing a full copy per entry. ## What the compilers say Both Clang and GCC diagnose it under `-Wall`: - Apple clang 21: "loop variable 'e' of type 'const std::pair<std::string, Order> &' (aka 'const pair<basic_string<char>, Order> &') binds to a temporary constructed from type 'reference' (aka 'pair<const std::string, Order> &') [-Wrange-loop-construct]"; - GCC's `-Wrange-loop-construct` is described in its option table as "Warn when a range-based for-loop is creating unnecessary copies" and is enabled by `-Wall`. ## The `auto` fix and what each form deduces `auto` deduces from the iterator's actual reference type, so it cannot get the key's `const` wrong: | Loop variable | Deduced | Effect | |---|---|---| | `const auto& e` | `const std::pair<const std::string, Order>&` | binds to the element, read-only | | `auto& e` | `std::pair<const std::string, Order>&` | binds; `e.second` is writable, `e.first` is not | | `auto e` | `std::pair<const std::string, Order>` | a copy per iteration, made explicit | | `auto&& e` | a reference to the element | binds to anything; covered with forwarding references | ## Structured bindings in the loop C++17 structured bindings give the two halves names: `for (const auto& [id, order] : book)`. The rules of what is deduced are easy to misstate: - the `auto` and its qualifiers apply to a **hidden variable**, here `const auto& e` bound to the element, not to `id` and `order` themselves; - `id` and `order` are **names for the members** of that hidden object; they are not separate variables or copies; - `decltype(id)` is the member's type as the binding declaration specifies it: with `auto& [sym, qty]` over a `std::map<std::string, int>`, `decltype(sym)` is `const std::string` (the key's own `const`) and `decltype(qty)` is `int`, and `qty += 1` writes into the map; - with plain `auto [s, q] = fill;` the hidden variable is a **copy** of `fill`, and the names refer into the copy. So the qualifiers you write govern whether the entry is copied, and the member types, including the key's `const`, come through unchanged. How `std::pair` and `std::tuple` support bindings in general is a separate topic. ## The same trap outside loops The mismatch is not specific to range-for. Any reference declared with a hand-typed pair binds to a converted temporary: - `const std::pair<std::string, Order>& top = *book.begin();` copies the first entry; the temporary lives as long as `top`, so it is not dangling, but changes to the map are invisible through it; - `std::unordered_map` has the same `std::pair<const Key, T>` element type and the same problem; - `for (auto [id, order] : book)` is the structured-binding version of the copy: plain `auto` makes the hidden variable a copy of each element, so it also copies every entry, and Apple clang 21 printed no warning for it even with `-Wall -Wextra`. ## Applying it in a trading gateway In an order book keyed by order id, a loop that sums exposure over all resting orders runs on every market-data tick. With the hand-written pair type, each tick copies every id string and every `Order`; with `const auto&` or `const auto& [id, order]`, it copies nothing. 1. Prefer `const auto&` or `const auto& [k, v]` for read-only iteration over associative containers. 2. Use `auto&` or `auto& [k, v]` when you update mapped values in place. 3. If you must write the type, use the container's own alias, `const decltype(book)::value_type&`, rather than retyping the pair.

  • In `for (auto& [sym, qty] : positions)` over a `std::map<std::string, int>`, can you assign to `sym`?
    No. `sym` names the `first` member of the bound element, whose type is `const std::string`, so `decltype(sym)` is `const std::string` and assignment is rejected. `qty` names `second`, an `int`, and `qty += 1` updates the map in place, because the hidden variable is a reference to the element.
  • Does `auto [s, q] = fill;` let you modify `fill` through `s`?
    No. With plain `auto`, the structured binding declaration introduces a hidden copy of `fill`, and `s` and `q` name members of that copy. Changes through them do not reach `fill`; write `auto& [s, q] = fill;` to bind to the original object.

  • A map's value_type is std::pair<Key, T> without const.
  • Binding a const reference never creates a copy.
  • Structured bindings are always copies of the members.
  • auto& [k, v] over a map allows modifying the key.
  • The type mismatch would be a compile error, so the loop must be fine.