title: How to implement standard iterators in class
body: <p>I have classes which are usually using standard containers as underlying fields. For example, I have a class</p>
  
  <pre><code>template &lt;typename T&gt;
  class Vec_3D
  {
  public:
      /* ... */
      std::array&lt;T, 3&gt; vec;
      /* ... */
  };
  </code></pre>
  
  <p>which has only one variable <code>vec</code> and the rest are just functions I need when working with vectors. I want to be able to use range-based for loop such as</p>
  
  <pre><code>Vec_3D&lt;double&gt; vec;
  for (double val : vec) {/*...*/}
  </code></pre>
  
  <p>which should obviusly iterate over <code>std::array&lt;double, 3&gt;</code>.</p>
  
  <p><strong>How to implement iterators in my class which should in turn call iterators of <code>std::array&lt;T, 3&gt;</code>?</strong></p>
  
  <p>I started with <a href="https://stackoverflow.com/questions/3582608/how-to-correctly-implement-custom-iterators-and-const-iterators">this question</a> and tried to define iterators in my class as</p>
  
  <pre><code>typedef std::iterator&lt;std::random_access_iterator_tag, T, ptrdiff_t, T*, T&amp;&gt; iterator;
  typedef std::iterator&lt;std::random_access_iterator_tag, const T, ptrdiff_t, const T*, const T&amp;&gt; const_iterator;
  
  inline iterator begin() noexcept { return vec.begin(); }
  inline const_iterator cbegin() const noexcept { return vec.cbegin(); }
  inline iterator end() noexcept { return vec.end(); }
  inline const_iterator cend() const noexcept { return vec.end(); }
  </code></pre>
  
  <p>but got compiling errors</p>
  
  <pre><code>error: no match for ‘operator!=’ (operand types are ‘Vec_3D&lt;double&gt;::iterator {aka std::iterator&lt;std::random_access_iterator_tag, double, long int, double*, double&amp;&gt;}’ and ‘Vec_3D&lt;double&gt;::iterator {aka std::iterator&lt;std::random_access_iterator_tag, double, long int, double*, double&amp;&gt;}’)
  </code></pre>
  
  <p>and <code>operator++, operator*</code></p>