{"id":6516,"date":"2023-02-19T16:59:02","date_gmt":"2023-02-19T16:59:02","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/the-iterator-protocol\/"},"modified":"2023-08-20T14:47:09","modified_gmt":"2023-08-20T14:47:09","slug":"the-iterator-protocol","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/the-iterator-protocol\/","title":{"rendered":"The Iterator Protocol"},"content":{"rendered":"<p>When you want to use a user-defined type in a range-based for-loop, your user-defined type has to implement the Iterator Protocol.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6503\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/ClassIdioms.png\" alt=\"ClassIdioms\" width=\"650\" height=\"335\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/ClassIdioms.png 1224w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/ClassIdioms-300x154.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/ClassIdioms-1024x527.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/ClassIdioms-768x395.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>Here is the question I want to answer: What interface must a user-defined type support to be usable in a range-based for-loop.<\/p>\n<h2>Requirements of a Range-Based for-Loop<\/h2>\n<p>Let me start with a simple experiment and use&nbsp;<code> std::array<\/code> in <a href=\"https:\/\/cppinsights.io\/s\/232581c8\">C++ Insights<\/a>. Here is a simple example:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ iteratorProtocol.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;array&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n   \r\n    std<span style=\"color: #555555;\">::<\/span>array<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #ff6600;\">5<\/span><span style=\"color: #555555;\">&gt;<\/span> myArr{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">4<\/span>, <span style=\"color: #ff6600;\">5<\/span>};\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> a<span style=\"color: #555555;\">:<\/span> myArr) a;\r\n  \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/cppinsights.io\/s\/232581c8\">C++ Insights<\/a> creates the following code out of it:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #009999;\">#include &lt;array&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>()\r\n{\r\n  std<span style=\"color: #555555;\">::<\/span>array<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #ff6600;\">5<\/span><span style=\"color: #555555;\">&gt;<\/span> myArr <span style=\"color: #555555;\">=<\/span> {{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">4<\/span>, <span style=\"color: #ff6600;\">5<\/span>}};\r\n  {\r\n    std<span style=\"color: #555555;\">::<\/span>array<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #ff6600;\">5<\/span><span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #555555;\">&amp;<\/span> __range1 <span style=\"color: #555555;\">=<\/span> myArr;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #555555;\">*<\/span> __begin1 <span style=\"color: #555555;\">=<\/span> __range1.begin();\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #555555;\">*<\/span> __end1 <span style=\"color: #555555;\">=<\/span> __range1.end();\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span>(; __begin1 <span style=\"color: #555555;\">!=<\/span> __end1; <span style=\"color: #555555;\">++<\/span>__begin1) {\r\n      <span style=\"color: #007788; font-weight: bold;\">int<\/span> a <span style=\"color: #555555;\">=<\/span> <span style=\"color: #555555;\">*<\/span>__begin1;\r\n      a;\r\n    }\r\n    \r\n  }\r\n  <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #ff6600;\">0<\/span>;\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Let me write it more generally: When you use a range-based for-loop (<code>for(range_declaration : range_expression)<\/code>), the compiler creates the following code:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\">{\r\n  auto &amp;&amp; __range = range_expression ;\r\n  auto __begin = <span style=\"color: #ff0000;\">begin_expr;<\/span>      \r\n  auto __end = <span style=\"color: #ff0000;\">end_expr;<\/span>          \r\n  for (;__begin <span style=\"color: #ff0000;\">!=<\/span> __end; <span style=\"color: #ff0000;\">++<\/span>__begin) {\r\n    range_declaration = <span style=\"color: #ff0000;\">*<\/span>__begin;\r\n    loop_statement\r\n  }\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I marked the essential parts in red:<\/p>\n<ul>\n<li>\n<p><code>begin_expr <\/code>and <code>end_expr<\/code>: return an iterator object<\/p>\n<\/li>\n<li>Iterator object\n<ul>\n<li><code>operator++<\/code>: incrementing the iterator<\/li>\n<li><code>operator*<\/code>: dereferencing the iterator and accessing the current element<\/li>\n<li><code>operator!=<\/code>: comparing the iterator with another iterator<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><code>begin_expr<\/code> and <code>end_expr<\/code> call the essential <code>begin<\/code> and <code>end<\/code> on the range_expression. begin and end could either be member functions or free functions on range_expression.<\/p>\n<p>Let me apply the theory and create a number generator.<\/p>\n<h2>A Generator<\/h2>\n<p>My first implementation supports the Iterator Protocol<\/p>\n<h3>The Iterator Protocol<\/h3>\n<p>The following class <code>Generator<\/code> supports the elementary Iterator Protocol.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ iterator.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Generator<\/span> {\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> begin_{};\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> end_{};\r\n\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    Generator(<span style=\"color: #007788; font-weight: bold;\">int<\/span> begin, <span style=\"color: #007788; font-weight: bold;\">int<\/span> end) <span style=\"color: #555555;\">:<\/span> begin_{begin}, end_{end} {}\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Iterator<\/span> {\r\n        <span style=\"color: #007788; font-weight: bold;\">int<\/span> value_{};\r\n    <span style=\"color: #9999ff;\">public:<\/span>\r\n        <span style=\"color: #006699; font-weight: bold;\">explicit<\/span> Iterator(<span style=\"color: #007788; font-weight: bold;\">int<\/span> pos) <span style=\"color: #555555;\">:<\/span> value_{pos} {}\r\n\r\n        <span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span><span style=\"color: #555555;\">*<\/span>() <span style=\"color: #006699; font-weight: bold;\">const<\/span> { <span style=\"color: #006699; font-weight: bold;\">return<\/span> value_; }           <em><span style=\"color: #0099ff;\">\/\/ (3)<\/span><\/em>\r\n\r\n        Iterator<span style=\"color: #555555;\">&amp;<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span><span style=\"color: #555555;\">++<\/span>() {                           <em><span style=\"color: #0099ff;\">\/\/ (4)<\/span><\/em>\r\n            <span style=\"color: #555555;\">++<\/span>value_;\r\n            <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #555555;\">*<\/span><span style=\"color: #006699; font-weight: bold;\">this<\/span>;\r\n        }\r\n\r\n        <span style=\"color: #007788; font-weight: bold;\">bool<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span><span style=\"color: #555555;\">!=<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> Iterator<span style=\"color: #555555;\">&amp;<\/span> other) <span style=\"color: #006699; font-weight: bold;\">const<\/span> {      <em><span style=\"color: #0099ff;\">\/\/ (5)<\/span><\/em>\r\n            <span style=\"color: #006699; font-weight: bold;\">return<\/span> value_ <span style=\"color: #555555;\">!=<\/span> other.value_;\r\n        }\r\n    };\r\n\r\n    Iterator begin() <span style=\"color: #006699; font-weight: bold;\">const<\/span> { <span style=\"color: #006699; font-weight: bold;\">return<\/span> Iterator{begin_}; }     <em><span style=\"color: #0099ff;\">\/\/ (1)<\/span><\/em>\r\n    Iterator end() <span style=\"color: #006699; font-weight: bold;\">const<\/span> { <span style=\"color: #006699; font-weight: bold;\">return<\/span> Iterator{end_}; }         <em><span style=\"color: #0099ff;\">\/\/ (2)<\/span><\/em>\r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n   std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n   Generator gen{<span style=\"color: #ff6600;\">100<\/span>, <span style=\"color: #ff6600;\">110<\/span>};\r\n   <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> v <span style=\"color: #555555;\">:<\/span> gen) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> v <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n\r\n   std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"\\n\\n\"<\/span>;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The class <code>Generator<\/code> has member functions <code>begin<\/code> and <code>end, <\/code>(lines 1 and 2)<code> <\/code>returning iterator objects, initialized with <code>begin_<\/code> and<code> end_. begin_<\/code> and <code>end_<\/code> stand for the range of created numbers. Let me analyze the inner class <code>Iterator<\/code> which keeps track of the generated numbers:<code> <\/code><\/p>\n<ul>\n<li><code>operator*<\/code> returns the current value<\/li>\n<li><code>operator++<\/code> increments the current value<\/li>\n<li><code>operator!=<\/code> compares the current value with the <code>end_<\/code> marker.<\/li>\n<\/ul>\n<p>Finally, here is the output of the program:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6514\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/iterator.png\" alt=\"iterator\" width=\"350\" height=\"193\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/iterator.png 439w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/iterator-300x165.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Let me generalize the iterator returned by<code> begin()<\/code> and <code>end()<\/code> and make it a forward iterator. Afterward, the class <code>Generator<\/code> can be used in most of the algorithms of the Standard Template Library. To put it differently, the <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\">unordered associative containers <\/a>support a forward iterator.<\/p>\n<\/p>\n<h3>A Forward Iterator<\/h3>\n<p>The following improved <code>Generator<\/code> has an inner class Iterator that is a forward iterator.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ forwardIterator.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;numeric&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Generator<\/span> {\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> begin_{};\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> end_{};\r\n\r\n <span style=\"color: #9999ff;\">public:<\/span>\r\n    Generator(<span style=\"color: #007788; font-weight: bold;\">int<\/span> begin, <span style=\"color: #007788; font-weight: bold;\">int<\/span> end) <span style=\"color: #555555;\">:<\/span> begin_{begin}, end_{end} {}\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Iterator<\/span> {\r\n        <span style=\"color: #006699; font-weight: bold;\">using<\/span> iterator_category <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>forward_iterator_tag;    <em><span style=\"color: #0099ff;\">\/\/ (1)<\/span><\/em>\r\n        <span style=\"color: #006699; font-weight: bold;\">using<\/span> difference_type   <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span><span style=\"color: #007788; font-weight: bold;\">ptrdiff_t<\/span>;\r\n        <span style=\"color: #006699; font-weight: bold;\">using<\/span> value_type        <span style=\"color: #555555;\">=<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span>;\r\n        <span style=\"color: #006699; font-weight: bold;\">using<\/span> pointer           <span style=\"color: #555555;\">=<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">*<\/span>;\r\n        <span style=\"color: #006699; font-weight: bold;\">using<\/span> reference         <span style=\"color: #555555;\">=<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&amp;<\/span>;\r\n        <span style=\"color: #007788; font-weight: bold;\">int<\/span> value_{};\r\n     <span style=\"color: #9999ff;\">public:<\/span>\r\n        <span style=\"color: #006699; font-weight: bold;\">explicit<\/span> Iterator(<span style=\"color: #007788; font-weight: bold;\">int<\/span> pos) <span style=\"color: #555555;\">:<\/span> value_{pos} {}\r\n\r\n        value_type <span style=\"color: #006699; font-weight: bold;\">operator<\/span><span style=\"color: #555555;\">*<\/span>() <span style=\"color: #006699; font-weight: bold;\">const<\/span> { <span style=\"color: #006699; font-weight: bold;\">return<\/span> value_; }\r\n        pointer <span style=\"color: #006699; font-weight: bold;\">operator<\/span><span style=\"color: #555555;\">-&gt;<\/span>() { <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #555555;\">&amp;<\/span>value_; }                <em><span style=\"color: #0099ff;\">\/\/ (2)<\/span><\/em>         \r\n\r\n        Iterator<span style=\"color: #555555;\">&amp;<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span><span style=\"color: #555555;\">++<\/span>() {                           \r\n            <span style=\"color: #555555;\">++<\/span>value_;\r\n            <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #555555;\">*<\/span><span style=\"color: #006699; font-weight: bold;\">this<\/span>;\r\n        }\r\n        Iterator <span style=\"color: #006699; font-weight: bold;\">operator<\/span><span style=\"color: #555555;\">++<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span>) {                             <em><span style=\"color: #0099ff;\"> \/\/ (3)<\/span><\/em>\r\n            Iterator tmp <span style=\"color: #555555;\">=<\/span> <span style=\"color: #555555;\">*<\/span><span style=\"color: #006699; font-weight: bold;\">this<\/span>; \r\n            <span style=\"color: #555555;\">++<\/span>(<span style=\"color: #555555;\">*<\/span><span style=\"color: #006699; font-weight: bold;\">this<\/span>); \r\n            <span style=\"color: #006699; font-weight: bold;\">return<\/span> tmp; \r\n        }\r\n                                                                <em><span style=\"color: #0099ff;\">\/\/ (4)<\/span><\/em>\r\n        <span style=\"color: #006699; font-weight: bold;\">friend<\/span> <span style=\"color: #007788; font-weight: bold;\">bool<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span><span style=\"color: #555555;\">==<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> Iterator<span style=\"color: #555555;\">&amp;<\/span> fir, <span style=\"color: #006699; font-weight: bold;\">const<\/span> Iterator<span style=\"color: #555555;\">&amp;<\/span> sec) {      \r\n            <span style=\"color: #006699; font-weight: bold;\">return<\/span> fir.value_ <span style=\"color: #555555;\">==<\/span> sec.value_;\r\n        }\r\n        <span style=\"color: #006699; font-weight: bold;\">friend<\/span> <span style=\"color: #007788; font-weight: bold;\">bool<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span><span style=\"color: #555555;\">!=<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> Iterator<span style=\"color: #555555;\">&amp;<\/span> fir, <span style=\"color: #006699; font-weight: bold;\">const<\/span> Iterator<span style=\"color: #555555;\">&amp;<\/span> sec) {      \r\n            <span style=\"color: #006699; font-weight: bold;\">return<\/span> fir.value_ <span style=\"color: #555555;\">!=<\/span> sec.value_;\r\n        }\r\n    };\r\n\r\n    Iterator begin() <span style=\"color: #006699; font-weight: bold;\">const<\/span> { <span style=\"color: #006699; font-weight: bold;\">return<\/span> Iterator{begin_}; }     \r\n    Iterator end() <span style=\"color: #006699; font-weight: bold;\">const<\/span> { <span style=\"color: #006699; font-weight: bold;\">return<\/span> Iterator{end_}; }         \r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n    Generator gen{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">11<\/span>};\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> v <span style=\"color: #555555;\">:<\/span> gen) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> v <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;                 <em><span style=\"color: #0099ff;\"> \/\/ (5)\r\n<\/span><\/em>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\r\n                                                              <em><span style=\"color: #0099ff;\"> \/\/ (6)<\/span><\/em>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sum:  \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>accumulate(std<span style=\"color: #555555;\">::<\/span>begin(gen), std<span style=\"color: #555555;\">::<\/span>end(gen), <span style=\"color: #ff6600;\">0<\/span>);\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\r\n                                                               <em><span style=\"color: #0099ff;\"> \/\/ (7)<\/span><\/em>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"prod: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>accumulate(gen.begin(), gen.end(), <span style=\"color: #ff6600;\">1<\/span>, \r\n                                             [](<span style=\"color: #007788; font-weight: bold;\">int<\/span> fir, <span style=\"color: #007788; font-weight: bold;\">int<\/span> sec){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> fir <span style=\"color: #555555;\">*<\/span> sec; });\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>First, <code>Iterator<\/code> needs several type aliases in the following member function declarations. Additionally, to the previous <code>Iterator<\/code> implementation in the program <code>iterator.cpp<\/code>, the current Iterator supports the following member functions: the arrow operator (<code>operator-&gt;<\/code> in line 2), the post-increment operator (<code>operator++(int)<\/code> in line 3), and the equal operator (<code>operator==<\/code> in line 4).<\/p>\n<p>That was it already. Now, I can use my improved <code>Generator<\/code> still in a range-based for-loop (line 5), but also in the STL algorithm <code>std::accumulate<\/code>. Line 6 calculates the sum of all numbers from 1 to 10; line 7 does a similar job by multiplying numbers from 1 to 11. In the first case, I choose the neutral element 0 for the summation, and in the second case the neutral element 1 for the multiplication.<\/p>\n<p>There is a subtle difference between the first and the second call of <code>std::accumulate<\/code>. The first call uses the non-member functions <code>std::begin<\/code> and <code>std::end <\/code>on the Generator: <code>std<span style=\"color: #555555;\">::<\/span>accumulate(std<span style=\"color: #555555;\">::<\/span>begin(gen), std<span style=\"color: #555555;\">::<\/span>end(gen), <span style=\"color: #ff6600;\">0<\/span>)<\/code>, but the second call the <code>Generator'<\/code>s member functions <code>begin()<\/code> and <code>end()<\/code> directly which I implemented.<\/p>\n<p>Finally, here is the output of the program:<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6515\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/forwardIterator.png\" alt=\"forwardIterator\" width=\"400\" height=\"281\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/forwardIterator.png 442w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/forwardIterator-300x210.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<h2>What&#8217;s Next?<\/h2>\n<p>In my next post, I will write about the Covariant Return Type. The Covariant Return Type of a member function allows an overriding member function to return a <em>narrower<\/em> type. This is particularly useful when you implement the creational design pattern <a href=\"https:\/\/en.wikipedia.org\/wiki\/Prototype\">Prototype<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you want to use a user-defined type in a range-based for-loop, your user-defined type has to implement the Iterator Protocol.<\/p>\n","protected":false},"author":21,"featured_media":6503,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[379],"tags":[414],"class_list":["post-6516","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-patterns","tag-iterator"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6516","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=6516"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6516\/revisions"}],"predecessor-version":[{"id":8110,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6516\/revisions\/8110"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6503"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}