Table-Driven Finite State Machine I'm writing a finite state machine based on a transition table in C++ 14.
My code so far looks like:

#include &lt;array&gt;
#include &lt;functional&gt;

template &lt; typename _State, typename _Event &gt;
class FSM {
  static constexpr size_t state_count = static_cast&lt;size_t&gt;(_Event::_);
  static constexpr size_t event_count = static_cast&lt;size_t&gt;(_State::_);

  using _FuncPtr = typename std::function&lt;_State(_State)&gt;;
  using _Table   = typename std::array&lt; std::array&lt; _FuncPtr, state_count &gt;, event_count &gt;;

  _State _current;
  _Table _transitions;

public:
  FSM(_State initial, _Table transitions) : _current(initial), _transitions(transitions) {};

  void operator()(const _Event&amp; event) {
    _current = _transitions[static_cast&lt;size_t&gt;(event)][static_cast&lt;size_t&gt;(_current)](_current);
  }

  _State state() const { return _current; }
};


The usage looks like:

/*            States &amp; Events            */
enum class State : char { Locked, Unlocked,    _ };
enum class Event : char { Pass, Coin,          _ };

/*              Transitions              */
State alarm(State current) {
  cout &lt;&lt; "Alarm" &lt;&lt; endl;
  return current;
}

State unlock(State current) {
  cout &lt;&lt; "Unlock" &lt;&lt; endl;
  return State::Unlocked;
}

State lock(State current) {
  cout &lt;&lt; "Lock" &lt;&lt; endl;
  return State::Locked;
}

State thankyou(State current) {
  cout &lt;&lt; "Thankyou" &lt;&lt; endl;
  return current;
}


void main() {
  auto fsm = FSM&lt; State, Event &gt;(State::Locked, {{
    // Locked    Unlocked
    {  alarm,    lock      },  // Pass
    {  unlock,   thankyou  }   // Coin
  }});

  // To switch states
  fsm(Event::Coin);
  fsm(Event::Pass);
}


Is there some way I can further simplify it?
I see a number of things that may help you improve your code.

Don't use leading underscores in names

Anything with a leading underscore is a reserved name in C++ (and in C).  See this question for details.

Don't use void main()

The only function signatures allowed by the C++ standard both require an int return type. See this question for details.

Don't abuse using namespace std

Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid.  This sample doesn't explicitly have that, but it's implied by the way the usage code is written.

Fix the bug

Right now, the way the arrays are constructed and the way that they're used are reversed.  Try implementing a state machine in which the number of states does not equal the number of events and you'll quickly see what I mean.

Rethink the interface

Right now, there is very little benefit in using the FSM class.  All of the actual code to implement the finite state machine is done with freestanding functions rather than member functions and requires a pair of enum classes that each must have _ as the last named item.  Further, the event that caused the transition isn't passed to the function -- it's implicit.  All of this makes for a fragile design.  Take the classic example of a vending machine that dispenses items for fifteen cents and that accepts only nickels (5 cents) and dimes (10 cents).  We'd have something like this:

enum class State : char { Empty, Five, Ten, _ };
enum class Event : char { Nickel, Dime,   _ };

// Then the state machine
auto fsm = FSM&lt; State, Event &gt;(State::Empty, {{
    // Nickel       Dime
    { zeroToFive,  zeroToTen },    // Empty
    { fiveToTen,   fiveToFifteen},  // Five
    { tenToFifteen, tenToTwenty}   // Ten
}});


Now we still have to write six standalone functions to implement this!  That's not very convenient.  Worse, the argument each function gets isn't even used, so each function looks something like this:

State fiveToFifteen(State ) {
  std::cout &lt;&lt; "got a dime, now have 15 cents\nHere's your item!\n";
  return State::Empty;
}