#include <iostream>
using namespace std;
struct A
{
explicit operator bool() const
{
return true;
}
operator int()
{
return 0;
}
};
int main()
{
if (A())
{
cout << "true" << endl;
}
else
{
cout << "false" << endl;
}
}
我的期望是A()
根据上下文转换为bool
使用 my operator bool()
,因此 print true
。
但是,输出是false
,表明它operator int()
被调用了。
为什么我explicit operator bool
没有按预期调用?