Overengineering max(a, b): Mixed comparison functions, common references, and Rust's lifetime annotations

Jonathan Müller

90
advanced
11:00-12:30, Friday, 5th July 2024

max is a function that returns the maximum of two numbers. While it seems simple on the surface, there are some nuances if you want to make it fully generic. For starters, what is max(-1, 1u) or max(lvalue, prvalue)? Let's go on a journey of overengineering max. We'll implement better mixed comparison functions, discuss common types and common references, wish for time machines to correct language semantics, and end up implementing Rust-style lifetime annotations using template metaprogramming and lots and lots of macros. The end result is a true work of art that we may or may not be using in actual production code.

metaprogramming
references
value semantics

Jonathan Müller

Jonathan is a Software Engineer at think-cell where he maintains the core libraries. He is the author of C++ open source projects like type_safe, a library of safety utilities and foonathan/memory, a memory allocator library. More recently, he’s taken an interest in programming languages and compilers and has published lexy, a parser library, and lauf, a bytecode interpreter. He is also on the C++ standardization committee where he is the assistant chair for std::ranges.