This question is related to this question. The following code compiles fine VC9 compiler but gives the error when compied with Comeau online. Can anybody tell me which one is correct and what is the meaning of the error?
error: ambiguous "?" operation: second operand of type "TypesafeBool" can be converted to third operand type "bool", and vice versa TypesafeBool b = (1==1) ? f() : false;
class TypesafeBool
{
private:
bool m_bValue;
struct Bool_ {
int m_nValue;
};
typedef int Bool_::* bool_;
inline bool_ True() const { return &Bool_::m_nValue; }
inline bool_ False() const { return 0; }
public:
TypesafeBool( const bool bValue ) : m_bValue( bValue ){}
operator bool_() const { return m_bValue ? True() : False(); }
};
TypesafeBool f()
{
return TypesafeBool(true);
}
int main()
{
TypesafeBool b = (1==1) ? f() : false;
}
The error is that the ternary operator must have a single type, and your expression (1=1) ? f() : false has two types -- f() has type TypesafeBool and false has type bool. You can convert between them, but Comeau doesn't know which you want to use. To resolve it, cast one of the sides of the ternary to the type of the other: (1=1) ? f() : TypesafeBool(false).
Comeau is correct here, as while it's obvious to the observer what type the result should take, the ternary expression needs to have a single type on its own, without reference to what it's used in, and the type it should pick is ambiguous.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With