C++Now 2019 has ended
Back To Schedule
Tuesday, May 7 • 14:30 - 16:00
Rise of the State Machines

Log in to save this to your schedule, view media, leave feedback and see who's attending!

Feedback form is now closed.
In this case study, we will conduct a battle against different ways of implementing state machines in modern C++, each of which has different trade-offs and characteristics. We will use a connection [1] example to measure and compare varied aspects of each solution such as compilation time, run-time performance, memory usage, executable size, and readability.

In the first round, the Naive solutions will fight against Standard Template Library (STL) solutions. The Naive will be represented by the if/else and switch/enum, both of which could be classed as the 'old school' way of implementing state machines with implicit states defined by booleans and/or enums. On the other side, we will have STL, featured by C++17 - std::variant [2] and the newest addition to C++20 - coroutines [3]. These two can be used to demonstrate a new way of implementing state machines by leveraging modern C++ language features.

The winner will go on to take on the Boost libraries represented by Boost.Statechart [4] and Boost.MetaStateMachine (MSM) [5]. Both libraries are compliant with Unified Modeling Language (UML) standard [6] and have many additional features such as logging and/or serialization. Whilst Statechart is more run-time, the MSM represents a fully compile-time approach with minimal run-time overhead.

While our winners are recovering, we will introduce the final contender - [Boost].StateMachineLanguage (SML) library [7] - a C++14 version of Boost.MSM of which I'm the author and claimes to have much faster compilation times then its precursor. We will follow by some 'godbolting' (comparing a generated assembly) [8] of different dispatching techniques (branch, switch, jump table, fold expressions [9]) available in the library to illustrate the main power of the SML - the ability to change the feature set and behavior at compile-time.

After that, we will return to the final round, where the winner will battle against [Boost].SML.

At the end of this epic battle, the audience will walk out with a clear understanding of possible state machine implementations, their trade-offs, and with guidelines of what solutions suit what problems.

Let's get ready to rumble at C++Now 2019!

[1]: http://boost-experimental.github.io/sml/embo-2018/images/connection.png
[2]: http://en.cppreference.com/w/cpp/utility/variant
[3]: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/n4736.pdf
[4]: http://www.boost.org/doc/libs/release/doc/html/boost_statechart.html
[5]: http://www.boost.org/doc/libs/release/doc/html/boost_msm.html
[6]: https://www.omg.org/spec/UML/2.5.1/PDF
[7]: https://github.com/boost-experimental/sml(Boost.SML is not an official Boost library)
[8]: https://godbolt.org/g/HVavPU
[9]: http://en.cppreference.com/w/cpp/language/fold

avatar for Kris Jusiak

Kris Jusiak

Software Architect, Quantlab Financial
Kris is a Software Architect passionate about programming and who has worked in different industries over the years including telecommunications, games and most recently finance for Quantlab Financial, LLC. He has an interest in modern C++ development with a focus on performance and... Read More →

Tuesday May 7, 2019 14:30 - 16:00 MDT
Flug Auditorium