5 ms·
A difference is that the linux macro works for non-constant expressions as well. All the crazyness is in order to support both constant expressions (and keep t
by ploxiln 9y ago
A difference is that the linux macro works for non-constant expressions as well.
All the crazyness is in order to support both constant expressions (and keep them constant expressions) and non-constant expressions with the same max() macro.
- smitherfield 9y agoThat one-liner I posted works with both constant and non-constant expressions (and in fact is more likely to be evaluated at compile-time than the macro), is type-safe, and has no side effects assuming both arguments are arithmetic types or pointers (like all uses of `max` in the Linux kernel / C-language programs).
- andrepd 9y agoThat version does this. From cppreference (emphasis mine): >The constexpr specifier declares that it is possible to evaluate the value of the function or variable at compile time. Such variables and functions can then be used where only compile time constant expressions are allowed (provided that appropriate function arguments are given).
- ovao 9y agoIn C++, all the craziness and brittleness you find in the "C version" is actually encompassed entirely within the `constexpr` keyword. The compiler gets to deal with a lot of complexity to make that possible, but for users it is genuinely "that easy".