It seems that in c++ in purely boolean context, operator char*()
has higher precedence than operator bool() const
; and enabling c++11 mode and using explicit operator bool() const
doesn't help. Is this a bug in g++ or a misfeature in the language standard? Or is there a good reason for this crazy behavior that I am not seeing?
A simple demonstration of the issue:
#include <stdio.h>
struct A
{
char buf[512];
int err;
operator char* () { return buf; }
operator const char* () const { return buf; }
operator bool () const { return !err; }
// explicit operator bool () const { return !err; } // same problem
};
int main()
{
A a;
a.err = -42;
if (a) {
printf("lolwut?\n");
return 1;
}
return 0;
}