title: Would Iterator loop call end() for many times?
body: <pre><code>#include &lt;vector&gt;
  #include &lt;iostream&gt;
  
  class Range {
      typedef typename std::vector&lt;int&gt; Vec;
      typedef typename Vec::iterator Iterator;
  public:
      Range(Vec&amp; vec, const int start_id, const int size)
              : vec_{vec},
                it_begin_{vec_.begin() + start_id},
                it_end_  {vec_.begin() + start_id + size}
      {}
      Iterator&amp; begin() {return it_begin_;}
      Iterator&amp; end()   {return it_end_;}
  private:
      Vec&amp; vec_;
      Iterator it_begin_;
      Iterator it_end_;
  };
  
  int main()
  {
      std::vector&lt;int&gt; a;
      a.resize(100);
      Range range(a,0,10);
      for (auto it = range.begin(); it != range.end(); ++it) { // Line A
          std::cout &lt;&lt; it - range.begin() &lt;&lt; "\n"; // Line B
      }
  }
  </code></pre>
  
  <p><a href="https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(j:1,options:(colouriseAsm:&#39;0&#39;,compileOnChange:&#39;0&#39;),source:&#39;%23include+%3Cvector%3E%0A%23include+%3Ciostream%3E%0A%0Aclass+Range+%7B%0A++++typedef+typename+std::vector%3Cint%3E+Vec%3B%0A++++typedef+typename+Vec::iterator+Iterator%3B%0Apublic:%0A++++Range(Vec%26+vec,+const+int+start_id,+const+int+size)%0A++++++++++++:+vec_%7Bvec%7D,%0A++++++++++++++it_begin_%7Bvec_.begin()+%2B+start_id%7D,%0A++++++++++++++it_end_++%7Bvec_.begin()+%2B+start_id+%2B+size%7D%0A++++%7B%7D%0A++++Iterator%26+begin()+%7Breturn+it_begin_%3B%7D%0A++++Iterator%26+end()+++%7Breturn+it_end_%3B%7D%0Aprivate:%0A++++Vec%26+vec_%3B%0A++++Iterator+it_begin_%3B%0A++++Iterator+it_end_%3B%0A%7D%3B%0A%0Aint+main()%0A%7B%0A++++std::vector%3Cint%3E+a%3B%0A++++a.resize(100)%3B%0A++++Range+range(a,0,10)%3B%0A++++for+(auto+it+%3D+range.begin()%3B+it+!!%3D+range.end()%3B+%2B%2Bit)+%7B+//+Line+A%0A++++++++std::cout+%3C%3C+it+-+range.begin()+%3C%3C+%22%5Cn%22%3B+//+Line+B%0A++++%7D%0A%7D&#39;),l:&#39;5&#39;,n:&#39;1&#39;,o:&#39;C%2B%2B+source+%231&#39;,t:&#39;0&#39;)),k:50,l:&#39;4&#39;,n:&#39;0&#39;,o:&#39;&#39;,s:0,t:&#39;0&#39;),(g:!((h:compiler,i:(compiler:g62,filters:(b:&#39;0&#39;,commentOnly:&#39;0&#39;,directives:&#39;0&#39;,intel:&#39;0&#39;),options:&#39;-Ofast&#39;),l:&#39;5&#39;,n:&#39;0&#39;,o:&#39;%231+with+x86-64+gcc+6.2&#39;,t:&#39;0&#39;)),k:50,l:&#39;4&#39;,n:&#39;0&#39;,o:&#39;&#39;,s:0,t:&#39;0&#39;)),l:&#39;2&#39;,n:&#39;0&#39;,o:&#39;&#39;,t:&#39;0&#39;)),version:4" rel="nofollow noreferrer">Assembly here</a></p>
  
  <p>Suppose I use optimization (like <code>g++ -Ofast</code>).</p>
  
  <p>In Line A, would the program call <code>range.end()</code> for many times, instead of saving the value of range.end() and compare that value with it in every iteration of the loop?</p>
  
  <p>In Line B, would the program call <code>range.begin()</code> for many times, instead of saving the value of <code>range.begin()</code> for the whole loop and then subtract that value from it in every iteration of the loop?</p>