Operator overloading, structs

Leandro Lucarella llucax at gmail.com
Tue Jun 9 17:12:33 PDT 2009


Don, el 10 de junio a las 02:02 me escribiste:
> 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.

*it is* more general, that's exactly what you don't like about it =)

> 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.

I don't think is a *disaster*, but I agree that maybe the distintion can
be useful (I didn't though about that). That makes me think, why not to
disable implicit cast from bool to int? It's even defined what the value
of int x = true; should be? I guess there are plenty of cases where you
don't want that, but I can't think of anything now...

Of course, int to bool implicit cast should still be possible.

-- 
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
----------------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------------
Le pedí que me enseñe a usar el mouse
Pero solo quiere hablarme del Bauhaus
Le pregunté si era chorra o rockera
Me dijo "Gertrude Stein era re-tortillera"
Me hizo mucho mal la cumbiera intelectual



More information about the Digitalmars-d mailing list