11

You can overload operator true and false i looked at examples and found this http://msdn.microsoft.com/en-us/library/aa691312%28v=vs.71%29.aspx

I completely dont understand how they work. I know if i write if(obj) and true returns true then the if is executed. It doesnt matter what false returns. However how does false work? in that doc it is suggected that the && operator uses it. I wrote the code below. I dont know how to get && to compile. || gives me a compile error too. How do i get false to be called? and how do i get the && and || to work?

        var ts= new MyTimeSpan();// ts += ts;
        if (ts){ Console.WriteLine("Is true"); }
        else { Console.WriteLine("not true"); }
        //Console.WriteLine(ts && ts ? "Y" : "N");

    class MyTimeSpan
    {
        public static MyTimeSpan operator +(MyTimeSpan t, MyTimeSpan t2) { return new MyTimeSpan(); }
        public static bool operator true(MyTimeSpan t) { return true; }
        public static bool operator false(MyTimeSpan t) { return false; }
    }
  • 2
    What are you trying to do? What does it mean for a timespan to be true or false? – Mark Byers Mar 05 '11 at 10:10
  • 2
    @Mark: I was messing around with the + operator in my last question/example which led me to this and i used the same class. The name is a dummy, i am just learning how the ops work. –  Mar 05 '11 at 10:16
  • You haven't declared operators for & or | - looking at the documentation link you provided it states you need to declare these operators, they need to return MyTimeSpan objects _and_ you need to declare operators for true and false. The documentation also explains where true and false operators are used. – James Gaunt Mar 05 '11 at 10:17
  • @James: For some reason, i was thinking && as if left and right side is both true and didnt see how the logical and relates to it. Now i see it does a and between the two ops and checks if it is true. Which is weird to me. Cause 1 && 2 is illegal in C# and in C++ it is true as there is no logical AND being applied http://codepad.org/9iCaqzQ2 –  Mar 05 '11 at 10:58

3 Answers3

25

The defining property of a short circuiting operator is that it doesn't need to evaluate the right side if the left side already determines the result. For or if the left side is true the result will be true in any case and it's unnecessary to evaluate the right side. For and it's the same except that if the left side is false the result will be false.

You need to overload both | and true to get ||. And & and false to get &&.

a||b corresponds to something like op_true(a)?a:(a|b). So if the true operator returns true it does not need to evaluate the expression of b.

a&&b corresponds to something like op_false(a)?a:(a&b). So if the false operator returns true it does not need to evaluate the expression of b.

Overloading the short circuiting operators is useful when creating a custom boolean type, such as nullable bools(See DBBool)

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
  • This confused me horribly. I was thinking && as if left and right side is both true and didnt see how the logical and relates to it. Now i see it does a AND between the two values and checks if it is true. Which is weird to me. Cause 1 && 2 is illegal in C# and in C++ it is true as there is no logical AND being applied (1&2==0==false, in this c code it says its true) codepad.org/9iCaqzQ2 –  Mar 05 '11 at 11:00
  • you dont need to answer this one but i made my comment into a question http://stackoverflow.com/questions/5203498/why-does-c-and-operators-work-the-way-they-do –  Mar 05 '11 at 11:24
  • @acid My answer had a big mistake. Perhaps that confused you. – CodesInChaos Mar 05 '11 at 12:26
9

When overloading the true and false operators they don't just return true and false, they are used to determine if a value of your type is considered to be true or false.

If for example a zero value in your class represents false, then a non-zero value represents true:

public static bool operator true(MyTimeSpan t) { return t.Value != 0; }
public static bool operator false(MyTimeSpan t) { return t.Value == 0; }

As the operators are each others inverse, the compiler doesn't need to use both to determine if a value is true or false. If you write if(obj) the compiler will use the true operator to determine if the value is true.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • +1. Also see comment i left in my question or the nearly same comment on this answer http://stackoverflow.com/questions/5203093/how-does-operator-overloading-of-true-and-false-work/5203185#5203185 –  Mar 05 '11 at 11:00
  • you dont need to answer this one but i made my comment into a question http://stackoverflow.com/questions/5203498/why-does-c-and-operators-work-the-way-they-do –  Mar 05 '11 at 11:24
  • If X is some arbitrary type which overrides those operators but one knows nothing else about it, is there any way to determine whether X is "false" (as opposed to merely not being "true")? – supercat Jun 02 '14 at 17:21
  • @supercat: I'm not sure. I tried some different ways to test it, but I haven't found a way to make use of the `false` operator without also overloading the `&` operator. – Guffa Jun 02 '14 at 21:38
  • @Guffa: It's too bad `if` uses the `true` operator rather than attempting an implicit cast to `bool` or else having a syntax specifically to use the `true` and `false` operators [e.g. `if true (threeStateCOnditional) { codeIfTrue } else if false { codeIfFalse} else { codeIfNeither }`] The fact that a type supports short-circuited operators doesn't necessarily mean it should be directly usable as a condition. – supercat Jun 02 '14 at 22:41
1

You will need to override & operator for && and | or ||.

Somthing like this: (Just a dummy code)

public static MyTimeSpan operator &(MyTimeSpan t, MyTimeSpan s) { return t; }

public static MyTimeSpan operator |(MyTimeSpan t, MyTimeSpan s) { return t; }
Shekhar_Pro
  • 18,056
  • 9
  • 55
  • 79