The ternary is effectively happening before the resultant object is being placed into C
, so convertibility to the type of C
does not come into play here.
The ternary operator requires that one of the two types (it doesn't matter which) can be unambiguously converted into the other.
But, while both those types are convertible to base
, they are not convertible to each other, since they're siblings on the inheritance hierarchy.
It's no different really to the expression cond ? 42 : "42"
, though cond ? 42.1 : 42
would be okay since 42
can easily become a floating point 42.0
.
Since you're shoehorning the result into the base class anyway, one solution is to tell the ternary that, so that it knows the types are convertible:
base C(cond
? static_cast<base>(derived_a())
: static_cast<base>(derived_b())
);