Possible Duplicate:
Why is this “min” template of cpp-next at fault?
From another question I got this function template:
template <class T, class U>
auto min(T x, U y) -> decltype(x < y ? x : y) {
return x < y ? x : y;
}
It compiles and seems to work fine, but I’m unsure why it works. How can the return type be deduced at compile-time? — I would think it can be either T or U depending on which argument is smaller, and that can only be determined at run-time.
An expression of the form
a ? b : calways returns the same type whetherais true-valued or not. Ifbandcare of different types, then type promotion occurs, just like when3 + 4.2evaluates to7.2(viadouble(3) + 4.2).