title: Constexpr Iterator in C++
body: <p>I am interested in creating a very minimal constexpr container for a personal project. The most important thing I need is a container with true constexpr iterators. They are going to be added to the standard eventually (<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0858r0.html" rel="nofollow noreferrer">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0858r0.html</a>) but I would like to know how to implement them in current C++.</p>
  
  <p>Let's say I had a data structure like this:</p>
  
  <pre><code>template &lt;typename T, unsigned N&gt;
  struct array {
    const T data[N];
  
    template&lt;typename... Args&gt;
    constexpr array(const Args&amp;... args) : data{args...} {
  }
    struct array_iterator {
      T const* ptr;
  
      constexpr array_iterator(const T* ptr) : ptr(ptr) {}
  
      constexpr void operator++() { ++ptr; }
      constexpr void operator--() { --ptr; }
      constexpr T const&amp; operator* () const { return *ptr; }
      constexpr bool operator==(const array_iterator&amp; rhs) const { return *(*this) == *rhs; }
      constexpr bool operator!=(const array_iterator&amp; rhs) const { return !(*this == rhs); }
    };
  
    constexpr array_iterator begin() const { return array_iterator(data); }
    constexpr array_iterator end()   const { return array_iterator(data + N); }
  };
  </code></pre>
  
  <p>What I need is to be able to actually use the iterators in a constexpr context:</p>
  
  <pre><code>constexpr array&lt;int, 3&gt; arr(1, 2, 3);
  constexpr auto it = arr.begin();
  </code></pre>
  
  <p>But obviously, since I am messing around with the non-constexpr ptr sub-object, I encounter errors like so:</p>
  
  <pre><code>iterator.cpp:46:18: error: constexpr variable 'it' must be initialized by a
        constant expression
    constexpr auto it = arr.begin();
                   ^    ~~~~~~~~~~~
  iterator.cpp:46:18: note: pointer to subobject of 'arr' is not a constant
        expression
  iterator.cpp:45:27: note: declared here
    constexpr array&lt;int, 3&gt; arr(1, 2, 3);
                            ^
  </code></pre>
  
  <p>So what would be a minimal constexpr iterator for a container like array?</p>