Overengineering max(a, b)

Mixed comparison functions, common references, and Rust’s lifetime annotations

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.

See also