I thought I knew how the ?: operator worked, but today someone surprised me by telling me this code does not compile:
class A {
};
class B : public A {
};
class C : public A {
};
A* example1(bool condition) {
return condition?new B():new C();
}
LLVM says "Incompatible operand types (B* and C*)"
Doing this works fine:
A* example2(bool condition) {
return condition?new A():new C();
}
Searching a bit tells me that the second operand must be convertible into the third, or vice versa, which B and C obviously isn't.
But what is the logic behind this?
Why is it not "enough" that they can be converted to a common base type?
Why do they need to be converted into each other anyhow, isn't it enough to convert the result, if needed?
I'm sure all this has a very logical explanation, anyone who wants to enlighten me?