The danger would be unexpected results... when you create a D, you expect to get a D as the result. There is also the fact that there is a cast involved... it is a "safe" cast, but a cast none-the-less. An identical argument could be made for the first example... why doesn't the compiler just make d
and int
, since double
can be converted trivially and it has already decided that is the type based on the result for i
. Or what of the case where you have two sibling classes... should they both resolve to the common base?
If you want that code to compile, you can always explicitly cast the result of D()
so that both expressions yield the same type.
And for the language lawyer bit:
[decl.spec.auto]/7:
If the list of declarators contains more than one declarator, the type of each declared variable is determined as described above. If the type deduced for the template parameter U is not the same in each deduction, the program is ill-formed.