c++ - Why base class is not automatically deduced in the same "auto" declaration? -
below error related auto, understandable:
auto = int(), d = double(); // error: inconsistent deduction ‘auto’ however, why following victimized same error:
struct b {}; struct d : b {}; const auto &b1 = b(), &b2 = d(); // error: inconsistent deduction ‘auto’ having known that, b1 deduced const b&, can't compiler try making b2 const b& ? (i.e. kind of hazard can cause if b2 have been deduced const b& ?)
the danger unexpected results... when create d, expect d result. there fact there cast involved... "safe" cast, cast none-the-less. identical argument made first example... why doesn't compiler make d , int, since double can converted trivially , has decided type based on result i. or of case have 2 sibling classes... should both resolve common base?
if want code compile, can explicitly cast result of d() both expressions yield same type.
and language lawyer bit:
[decl.spec.auto]/7:
if list of declarators contains more 1 declarator, type of each declared variable determined described above. if type deduced template parameter u not same in each deduction, program ill-formed.
Comments
Post a Comment