Overload of ! operator

Jonathan M Davis jmdavisProg at gmx.com
Tue Jun 25 21:05:45 PDT 2013


On Wednesday, June 26, 2013 05:35:03 cal wrote:
> On Wednesday, 26 June 2013 at 02:50:51 UTC, Eric wrote:
> > Is there a way to overload the ! operator?  I can't seem to get
> > it to work with the standard unaryOp method.  I need this
> > because
> > I am making a wrapper for a C++ API that has ! overloaded.
> > 
> > -Eric
> 
> According to http://dlang.org/operatoroverloading.html#Cast, the
> following are rewritten:
> 
> if (e) => if (e.opCast!(bool))
> if (!e) => if (!e.opCast!(bool))
> 
> So perhaps you need to override opCast!(bool).

Yeah, that should work for the conditions in if, while, and for loops but 
won't work for anything else (_maybe_ ternary operators, but I'm not sure). 
So, if you need to be able to do !obj in the general case, that's not going to 
work, but if you only need it for conditions, then it would. And of course 
this all assumes that the C++ code is overloading ! to do something sane with 
bool rather than redefining it to mean something completely different.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list