There is no straight-up evaluate-struct-as-a-bool operator. That's why there's a way to give a struct an operator bool
method. It tells the compiler how to interpret a value of the given struct type when it's used in a bool
context.
The code you've shown would not use such an operator, though, since the operator applies to structs, not pointers to structs; you have an expression of type foo*
, not foo
. Pointers automatically convert to bool
because that's an intrinsic conversion the compiler knows how to perform. Null pointers are false, and non-null pointers are true. The code shown in the question would never yield the reported error message.
If you do indeed have a struct in a place where the compiler expects a bool
, and you cannot, for whatever reason, give the struct an operator bool
method, then you can perform the conversion the old-fashioned way and use an ordinary function:
bool interpret_foo_as_bool(foo const& f);
Implement the function however you want, and then call it, passing your foo
value. For example:
foo* x = ...;
int y = interpret_foo_as_bool(*x) ? 3 : 4;