25

Shortcut patterns: dispatch tables and maps

Three lightweight stand-ins for a Strategy, State, or Command hierarchy, taught through a shipment-tracking ingestion service.

Demo 25Source on GitHub ↗

Three dispatch shortcuts on a shipment-tracking ingestion service:

  • Function-pointer table (jump table). A constexpr array indexed by the message-type enum maps each type straight to its handler. The closed-set, lowest-overhead form.
  • Dispatch map. std::unordered_map<Key, handler> accepts late register_handler() calls, the open form for a set that grows at runtime. A C++23 variant uses std::flat_map.
  • Constexpr table. A std::array table whose completeness is asserted at compile time with static_assert.

Each form is contrasted against the object-oriented Strategy, State, and Command designs from earlier chapters, with the teaching point of when a table is enough and when you have outgrown it.

Build, run, observe

./scripts/build.sh cpp23
./scripts/verify.sh cpp23      # 14/14 tests pass
./examples/25-dispatch-tables/demo.sh cpp23

The demo routes a stream of shipment messages through all three forms, shows an unknown message type falling back to a defined handler rather than crashing, and registers a new handler into the open map at runtime. The index helper uses std::to_underlying on C++23 and a guarded static_cast on C++20. Builds standalone and under the cpp20 preset (12 tests).