...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
Front Page / Algorithms / Concepts / Inserter |
An Inserter is a compile-time substitute for STL Output Iterator. Under the hood, it's simply a type holding two entities: a state and an operation. When passed to a transformation algorithm, the inserter's binary operation is invoked for every element that would normally be written into the output iterator, with the element itself (as the second argument) and the result of the previous operation's invocation — or, for the very first element, the inserter's initial state.
Technically, instead of taking a single inserter parameter, transformation algorithms could accept the state and the "output" operation separately. Grouping these in a single parameter entity, however, brings the algorithms semantically and syntactically closer to their STL counterparts, significantly simplifying many of the common use cases.
In the following table and subsequent specifications, in is a model of Inserter.
Expression | Type |
---|---|
in::state | Any type |
in::operation | Binary Lambda Expression |
Expression | Semantics |
---|---|
in::state | The inserter's initial state. |
in::operation | The inserter's "output" operation. |