此问题与this问题有关。以下代码编译了精美的VC9编译器,但是在与Comeau在线编译时出现错误。任何人都可以告诉我哪一个是正确的,错误的含义是什么?隐式类型转换 - 编译错误
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;
}
我觉得Comeau在这种情况下是正确的。即使是g ++和Clang ++也会出现相同的错误。在g ++的情况下,我得到的错误是'错误:操作数为?:具有不同类型'TypesafeBool'和'bool' – 2010-08-20 09:42:13
构造函数应该明确的一个很好的示例 – mukeshkumar 2010-08-20 10:01:38