Below error related to auto, is understandable:
auto i = int(), d = double(); // error: inconsistent deduction for ‘auto’
However, why following is victimized with the same error:
struct B {};
struct D : B {};
const auto &b1 = B(), &b2 = D(); // error: inconsistent deduction for ‘auto’
Having known that, b1 is already deduced to const B&, can’t compiler try making b2 also a const B& ? (i.e. What kind of hazard it can cause if b2 would have been deduced to const B& ?)
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
dandint, sincedoublecan be converted trivially and it has already decided that is the type based on the result fori. 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: