Re:Maximal Min() and Max()
by peheje on Thursday August 08, 2024 @02:50PM
(#41194910)
Why don't they define a min/max for each primitive type?
How many are there, 15?
[ Parent
| on HN ]
The reason the current macro is so complex is because it supports mixed types while avoiding (failing on) integer promotion bugs. A version supporting arguments of all the same type would be just as trivial as in C++ (albeit relying on GCC extensions like statement expressions).
Even use a macro to define all typed min max macros
This is addressed if you chase down the original kernel mailing list thread (the "flamewar" linked in the article). It was important to Linus that the macro was actually min/max and not min_slong max_uint etc, so that people couldn't "accidentally" use the untyped version. In other words he was trying quite hard to "force" people to use these macros.
If you would then say "why doesn't min/max just implement a switch on each primitive type", I think on some level it does just do that.