I'm confused why the book I'm reading says that the following two declarations have the same effect:
template<typename T1, typename T2> auto max (T1 a, T2 b) -> decltype(b<a?a:b); template<typename T1, typename T2> auto max (T1 a, T2 b) -> decltype(true?a:b);
In the first declaration, the return type of
max(a, b) could be the type of
a, or the type of
b. In the second declaration, the return type is the type of
a. How can the two declarations have the same effect?
Here is the passage that I read:
In the second declaration, the return type is the type of
On what do you base that assumption? I think this is the source of the confusion. The evaluated static type of a ternary expression (or of any expression) does not depend on any semantic analysis of the values of the operands, only on the types of the operands. The types of all three ternary operands are the same in both expressions, thus the deduced type is the same.
In other words, the fact that the conditional operand is
true which, at runtime, would cause the expression to evaluate to
a, is completely irrelevant with regard to static type analysis and deduction.