How can I determine the largest number among three numbers using C++?
I need to simplify this
w=(z>((x>y)?x:y)?z:((x>y)?x:y));
A variant on oisyn's answer (use an initializer list) and Bathesheba's answer (invoke no copies) is to use std::ref to create an initializer list of references, and then use std::max
normally:
using std::ref;
w = std::max({ref(x), ref(y), ref(z)});
This is only advantageous if creating a reference is cheaper than creating a copy (and it isn't for primitives like int
)