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);
I do not understand why these two code snippets can have the same effect. Plz give me some hint and a underlying explanation.
Cheers.
Because the type returned by a ternary operator is decided according the types of the second and third arguments, not according the value of the first.
You can verify this with the following code
#include <type_traits>
int main ()
{
auto x = true ? 1 : 2l;
static_assert( std::is_same<decltype(x), long>::value, "!" );
}
Isn't important that true ? 1 : 2l
return ever 1
; the ternary operator return a common type between 1
(int
) and 2l
(long
). That is long
.
In other words: there isn't (at the moment) a constexpr
ternary operator.