The type of a conditional expression does not depend on whether the condition is true or not.
decltype(b<a?a:b)
is the type of the expression b<a?a:b
, which is always the same.
It is not either decltype(a)
or decltype(b)
depending on the value of b<a
.
Note that the expression you give to decltype
is never evaluated - only its type is determined, and it is determined at compile time.
Somewhat informally:
- if
a
can be converted to the type of b
, the expression has the same type as b
- if
b
can be converted to the type of a
, the expression has the same type as a
- otherwise, the expression is ill-typed.
(There's also a trtuckload of nitty-gritty details about standard conversions and yada-yada involved, but this is the gist of it.)
For instance, this will not compile becuse there are no valid conversions that could give notgood
a type:
int main()
{
decltype(true ? "hello" : 123.4) notgood;
}
while this will compile, and run, and be well-defined, because the invalid dereference is never evaluated:
int main()
{
decltype(*(int*)0 + 1)` x = 0;
return x;
}
decltype
expressions to yield different types? – Concordant