Search code examples
c++iteratorc++20c++-concepts

How to summarize the meaning of the C++20 iterator-concepts?


If I want to visualize the hierarchy of the C++20 iterator concepts I want to briefly explain in one line what each concept is about. Nothing precise, just a rough meaning (the precision is in the source... ;-) )

I have trouble coming up with a good "one-line summary" for

  • input_iterator
  • output_iterator
  • forward_iterator

Here is my visualization:

digraph iter_concepts {

rankdir = BT;

node[shape=record];

indirectly_readable [label="{indirectly_readable|... = *it}"];
indirectly_writable [label="{indirectly_writable|*it = ...}"];
weakly_incrementable [label="{weakly_incrementable|++it, one pass}"];
incrementable [label="{incrementable|++it, multi pass}"];
input_or_output_iterator [label="{input_or_output_iterator|*it}"];
sentinel_for [label="{sentinel_for|for(x : ...)}"];
sized_sentinel_for [label="{sized_sentinel_for|for(... i<sz ...)}"];
input_iterator [label="{input_iterator|}"];
output_iterator [label="{output_iterator|}"];
forward_iterator [label="{forward_iterator|}"];
bidirectional_iterator [label="{bidirectional_iterator| --it}"];
random_access_iterator [label="{random_access_iterator| it+=n, it-=n, it[n]}"];
contiguous_iterator [label="{contiguous_iterator| it ≍ pointer}"];


incrementable -> weakly_incrementable;
input_or_output_iterator -> weakly_incrementable;
sentinel_for -> input_or_output_iterator;
sized_sentinel_for -> sentinel_for;
input_iterator -> input_or_output_iterator;
input_iterator -> indirectly_readable;
output_iterator -> input_or_output_iterator;
output_iterator -> indirectly_writable;
forward_iterator -> input_iterator;
forward_iterator -> sentinel_for;
forward_iterator -> incrementable;
bidirectional_iterator -> forward_iterator;
random_access_iterator -> bidirectional_iterator;
contiguous_iterator -> random_access_iterator;

}

The resulting image is, which I am quite happy with, except for the missing boxes:

digraph iter_concepts

Update: I corrected the missing forward_iterator -> incrementable in the graph (not yet in the image).


Solution

    • input_iterator is an input_or_output_iterator that is also indirectly_readable. Nothing special about that, it's just a more "specialized"/refined concept
    • output_iterator is an input_or_output_iterator that is also indirectly_writable. Nothing special about that, it's just a more "specialized"/refined concept
    • forward_iterator is an input_iterator that is also incrementable and sentinel_for. Nothing special about that, it's just a more "specialized"/refined concept

    If you really need an example per box, just combine them intelligently to underline the sum of properties:

    • input_iterator -> ++it; auto&& ref = *it;
    • output_iterator -> ++it; *it = value;
    • forward_iterator -> auto bkp = it; ++it; auto&& ref1 = *it; auto&& ref0 = *bkp; ++bkp; *bkp == ref1;