Reverse iterator returns garbage when optimized : <p>I have a <code>AsIterator</code> template class which takes a numeric-like type, in this example just an <code>int</code>, and converts it into an iterator (<code>++</code> and <code>--</code> increment and decrement the number, and <code>operator*</code> just returns a reference to it).</p>

<p>This works fine <strong>unless it's wrapped into a <code>std::reverse_iterator</code> and compiled with any optimization</strong> (<code>-O</code> is enough). When I optimize the binary, the compiler strips out the dereference call to the <code>reverse_iterator</code> and replaces it with some weird value. It must be noted that it still <strong>makes the correct number of iterations</strong>. It's just the value obtained by reverse iterator that is garbage.</p>

<p>Consider the following code:</p>

<pre class="lang-cpp prettyprint-override"><code>#include &lt;iterator&gt;
#include &lt;cstdio&gt;

template&lt;typename T&gt;
class AsIterator : public std::iterator&lt;std::bidirectional_iterator_tag, T&gt; {
    T v;
public:
    AsIterator(const T &amp; init) : v(init) {}

    T &amp;operator*() { return v; }

    AsIterator &amp;operator++() { ++v; return *this; }
    AsIterator operator++(int) { AsIterator copy(*this); ++(*this); return copy; }
    AsIterator &amp;operator--() { --v; return *this; }
    AsIterator operator--(int) { AsIterator copy(*this); --(*this); return copy; }

    bool operator!=(const AsIterator &amp;other) const {return v != other.v;}
    bool operator==(const AsIterator &amp;other) const {return v == other.v;}
};

typedef std::reverse_iterator&lt;AsIterator&lt;int&gt;&gt; ReverseIt;

int main() {
    int a = 0, b = 0;
    printf("Insert two integers: ");
    scanf("%d %d", &amp;a, &amp;b);
    if (b &lt; a) std::swap(a, b);

    AsIterator&lt;int&gt; real_begin(a);
    AsIterator&lt;int&gt; real_end(b);
    for (ReverseIt rev_it(real_end); rev_it != ReverseIt(real_begin); ++rev_it) {
        printf("%d\n", *rev_it);
    }
    return 0;
}
</code></pre>

<p>This should supposingly loop down from the highest inserted number to the lowest and print them, such as in this run (compiled with <code>-O0</code>):</p>

<pre><code>Insert two integers: 1 4 
3
2
1
</code></pre>

<p>What I get with <code>-O</code> is instead:</p>

<pre><code>Insert two integers: 1 4 
1
0
0
</code></pre>

<p>You can <a href="http://cpp.sh/3htz">try it online here</a>; the numbers may vary but they're always "wrong" when optimizing the binary.</p>

<hr>

<p>What I've tried:</p>

<ul>
<li>hardcoding the input integers is enough to produce the same result;</li>
<li>the issue persists with <em>gcc 5.4.0</em> and <em>clang 3.8.0</em>, also when using <em>libc++</em>;</li>
<li>making all the objects <code>const</code> (i.e. returning <code>const int &amp;</code>, and declaring all variables as such) doesn't fix it;</li>
<li>using the <code>reverse_iterator</code> in the same way on for example some <code>std::vector&lt;int&gt;</code> works fine;</li>
<li>if I just use <code>AsIterator&lt;int&gt;</code> for a normal forward or backward loop it works fine.</li>
<li>in my tests, the constant <code>0</code> that is printed is actually <em>hardcoded</em> by the compiler, the calls to <code>printf</code> all look like this when compiled with <code>-S -O</code>:</li>
</ul>

<pre><code>    movl    $.L.str.2, %edi  # .L.str.2 is "%d\n"
    xorl    %eax, %eax
    callq   printf
</code></pre>

<p>Given the consistency of <em>clang</em> and <em>gcc</em>'s behavior here I am pretty sure they're doing it right and I misunderstood, but I really can't see it.</p>
