Overload of ! operator

Eric eric at makechip.com
Tue Jun 25 22:15:19 PDT 2013


On Wednesday, 26 June 2013 at 04:59:19 UTC, cal wrote:
> 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?

I should have also added that the overloaded ! method returns a 
class instance and not a bool.

-Eric





More information about the Digitalmars-d-learn mailing list