bearophile wrote:
Leandro Lucarella:
I think the more general solution is to allow multiple implicit cast
operators and simply provide implicit conversion to bool for the classes
you want to be able to do if (x) on.

No, it's not more general. You do NOT want to allow conversion to bool. The reason is that bool can itself be implicitly converted, eg this compiles:

   bool b = true;
   int y = b;

--
And that's a disaster:

struct Foo
{
  bool opImplicitCast() { return true; }
}

Foo x;
if (x) { ... }  // OK
int y = x;      // This would compile!!!!

C++ libraries go to a fair bit of trouble to allow if(x) without allowing x to be converted to bool.

Reply via email to