Back To Schedule

Unit Testing an Expression Template Library in C++20

09:00 - 10:30 Thursday 2nd May 2024 MT Bethe Hall
Intermediate
Advanced
Tooling/Utilities

Expression templates are one of the wonders that set C++ apart from all other programming languages. Using just the C++ type system, we can store arbitrary information in a type, giving us a way to calculate anything at compile-time. As years have gone by, we now have new ways of expressing compile-time computation, removing the need for relics of the past like expression templates. However, for some of us, expression templates remain a fascination, bordering on obsession.

Regardless of the paradigm, unit testing code at compile-time is no trivial matter. In very rare cases, a compiler's output can vary between compile-time and run-time, given identical source code. Therefore, in order to properly unit test a library meant for compile-time computation, we must run compile-time tests.

This talk will use an expression template parser combinator library as a case study to delve into the world of compile-time testing. Triggering a conditional miscompile is easy, but we all know that C++ compiler output can get notoriously unruly upon hitting errors. Therefore, we will explore ways to attain as much readability as possible in our diagnostics and error messages. Throughout the talk, we will contrast the popular unit testing libraries' solutions to compile-time testing, and we will learn which features are left to be desired. We will push the language to its limits to achieve the most user-friendly error messages we can get, while looking back on how far C++ has come in terms of compile-time programming. Ultimately, we will end up where all C++ users eventually stand: wanting more features out of the language. We will end with a roadmap of sorts, showing which future versions of C++ are slated to render our compile-time testing much simpler and more expressive than we can achieve today, and which desired features have yet to commence the standardization pipeline.

View Slides

Braden Ganetsky

Braden Ganetsky serves as the Secretary of WG21, the ISO C++ committee. Braden graduated from the University of Manitoba with a degree in mechanical engineering, fueled by his passion for mechanical puzzles. During his final year of school, when all classes and activities were remote, he discovered C++ and has been hooked ever since. He interned as a C++ developer, which turned into a full time job, completing a successful pivot of his career. Now he spend his nights working on fun projects like parser combinators, constexpr unit testing, and getting involved in the C++ community.