Do the following two declarations involving automatic return type work the same? If so, why?

  • A+
Category:Languages

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:

Do the following two declarations involving automatic return type work the same? If so, why?

Do the following two declarations involving automatic return type work the same? If so, why?

 


In the second declaration, the return type is the type of a.

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.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: