by evincarofautumn · 14y ago · 0 comments
template<class T>
const T& max(const T& a, const T& b) {
return a < b ? b : a;
}
template<class T, class Predicate>
const T& max(const T& a, const T& b, Predicate p) {
return p(a, b) ? b : a;
}
And your definition of MAX() is wrong, not just because of multiple evaluation of the arguments. The “conventionally wrong” MAX() would be this: #define MAX(a, b) ((a) < (b) ? (b) : (a))That said, for me, your code would be clear, as not being in all caps, I would assume it's a function and therefore doesn't have the same weakness.
As someone else pointed out, there's no way you can look at MAX(0,f()) and know that it's enforced that f() is only used once within the macro.