1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
class MyClass {
public:
explicit operator bool() const {
return true;
}
};
int main() {
MyClass obj;
// if (obj) { // Error: Implicit conversion to bool is not allowed
if (static_cast<bool>(obj)) { // OK: Explicit conversion is required
std::cout << "Object is true" << std::endl;
}
return 0;
}
Back to explicit