Overload of ! operator

cal callumenator at gmail.com
Tue Jun 25 21:59:14 PDT 2013


On Wednesday, 26 June 2013 at 04:06:05 UTC, Jonathan M Davis 
wrote:
> 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
...

import std.stdio;

struct S {
   int x;
   bool opCast(T)() if (is(T == bool)) {
     return x == 0;
   }
}

void main() {	
   auto s = S(1);
   auto b = !s;
   writeln(b);	// true
}

Is this not supposed to work?


More information about the Digitalmars-d-learn mailing list