If the following test-programm
#include <iostream>
class A {
public:
A() {}
explicit operator bool() const {
std::cout << __PRETTY_FUNCTION__ << std::endl;
return true;
}
// explicit operator bool() {
// std::cout << __PRETTY_FUNCTION__ << std::endl;
// return true;
// }
const operator int() const {
std::cout << __PRETTY_FUNCTION__ << std::endl;
return 1;
}
operator int() {
std::cout << __PRETTY_FUNCTION__ << std::endl;
return 1;
}
};
int main() {
A a;
if (a) {
std::cout << "bool()" << std::endl;
}
if (a + 0) {
std::cout << "int()" << std::endl;
}
}
is run, the output is
int A::operator int()
bool()
int A::operator int()
int()
and not
bool A::operator _Bool()
bool()
int A::operator int()
int()
what I expected (and what you get if you uncomment the commented parts).
So the question is what are the rules giving the conversion to non-const-int precedence over converting to const-bool?
See Question&Answers more detail:os