Simulate Turing machine I have written this code to simulate a Turing machine and wonder what could be improved. In particular, the --tape.end() seems a bit dodgy.

#include &lt;array&gt;
#include &lt;iostream&gt;
#include &lt;list&gt;


typedef enum {right, left} Direction;

template&lt;int n&gt;         // n is the number of symbols
struct State {
    std::array&lt;int, n&gt; write;       // Value to write, 0 &lt;= write &lt; n
    std::array&lt;Direction, n&gt; move;  // Direction to move tape
    std::array&lt;State*, n&gt; next;     // Next state, nullptr represents HALT
};

// Some test machines
std::array&lt;State&lt;2&gt;, 5&gt; A = {{
    {{1, 1}, {left, left},   {&amp;A[1],   &amp;A[0]}},
    {{1, 1}, {right, right}, {&amp;A[2],   &amp;A[1]}},
    {{1, 1}, {left, right},  {&amp;A[0],   &amp;A[3]}},
    {{1, 1}, {left, right},  {&amp;A[0],   &amp;A[4]}},
    {{1, 0}, {left, right},  {nullptr, &amp;A[2]}},
}};

std::array&lt;State&lt;2&gt;, 6&gt; B = {{
    {{1, 1}, {left, left},   {&amp;B[1], &amp;B[0]}},
    {{1, 1}, {right, right}, {&amp;B[2], &amp;B[1]}},
    {{0, 1}, {right, right}, {&amp;B[5], &amp;B[3]}},
    {{1, 0}, {left, right},  {&amp;B[0], &amp;B[4]}},
    {{0, 1}, {left, right},  {&amp;B[0], &amp;B[2]}},
    {{1, 1}, {left, left},   {&amp;B[4], nullptr}},
}};

std::array&lt;State&lt;3&gt;, 3&gt; C = {{
    {{1, 1, 2}, {right, left, left},  {&amp;C[1], &amp;C[1], &amp;C[0]}},
    {{1, 1, 2}, {left, right, right}, {&amp;C[0], &amp;C[2], nullptr}},
    {{0, 2, 1}, {left, right, left},  {&amp;C[0], &amp;C[2], &amp;C[2]}},
}};


int main() {
    std::list&lt;int&gt; tape = {0};
    std::list&lt;int&gt;::iterator head = tape.begin();
    State&lt;2&gt;* current_state = &amp;A[0];
    int read;

    unsigned long long int step_count = 0;
    while (true) {
        step_count += 1;
        // Read tape
        read = *head;
        // Write to tape
        *head = current_state-&gt;write[read];
        // Move tape head
        if (current_state-&gt;move[read] == right) {
            if (head == --tape.end()) {
                tape.push_back(0);
            }
            head++;
        } else if (current_state-&gt;move[read] == left) {
            if (head == tape.begin()) {
                tape.push_front(0);
            }
            head--;
        }
        // Change state
        if (current_state-&gt;next[read] == nullptr) {
            break;
        } else {
            current_state = current_state-&gt;next[read];
        }
    }
    std::cout &lt;&lt; "Halted after " &lt;&lt; step_count &lt;&lt; " steps." &lt;&lt; std::endl;
    unsigned int sum = 0;
    for (int it : tape) {
        sum += (it != 0);
    }
    std::cout &lt;&lt; "Total non-zeroes: " &lt;&lt; sum &lt;&lt; std::endl;
    return 0;
}
I believe you're looking for std::prev:

std::prev(tape.end());


The linked page even states the exact comparison of the two:


  Although the expression --c.end() often compiles, it is not guaranteed to do so: c.end() is an rvalue expression, and there is no iterator requirement that specifies that decrement of an rvalue is guaranteed to work. In particular, when iterators are implemented as pointers, --c.end() does not compile, while std::prev(c.end()) does.


I do have some additional points as well:


typedef isn't needed for enums in C++.
Since tape is a storage container of a primitive type (ints), the default constructor will zero-initialize it automatically.  As such, you don't need to assign to {0}.

std::list&lt;int&gt; tape;


In case you want to verify it right away, take a look at this test.
It may be more useful to use auto with head:

auto head = tape.begin();


This allows for less typing and for any iterator type (if it ever changes).
You could put these "test machines" in a separate file or somewhere separate from the main implementation.  Proper unit testing may be preferred, but I don't have much to say about that.  I'd also rename them as such as it's best not to use single-variable names outside of loop counters.

Another note: you can omit the = for each std::array for C++11 list initialization.
main() seems to be handling too much of the logic.  Have other functions handle it and only have main() initialize variables, call these functions, then output the results.
return 0 or similar isn't needed at the very end of main(); it'll always do the same return at this point.