Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What type should an overload of the negation operator (!) have?

If i overload the ! operator in a class, what type should it return? In a book I found this (partial listing):

public class MyType {
    public int IntField { get; set; }

    public MyType(int intField) {
        IntField = intField;
    }

    public static bool operator !(MyType mt) {
        return (mt.IntField <= 0);
}

It does compile, but I would expect the ! operator to return a MyType instance, something like

public static MyType operator !(MyType mt) {
    var result = new MyType(-mt.IntField);
    return result;
}

In fact, I would expect the compiler to demand that the ! operator returns a MyType. But it doesn't.

So... why doesn't the return type of the ! operator have to be the containing type? You do have to make the return type of ++ or -- be the containing type.

like image 296
comecme Avatar asked Jun 21 '11 20:06

comecme


People also ask

Which operator is negation of in operator?

The logical negation operator ( ! ) reverses the meaning of its operand. The operand must be of arithmetic or pointer type (or an expression that evaluates to arithmetic or pointer type). The operand is implicitly converted to type bool .

What is the negation operator in C++?

The ! (logical negation) operator determines whether the operand evaluates to 0 (false) or nonzero (true). The expression yields the value 1 (true) if the operand evaluates to 0, and yields the value 0 (false) if the operand evaluates to a nonzero value.

Which operator is used to overload an operator?

Operator Overloading is the method by which we can change the function of some specific operators to do some different task. In the above syntax Return_Type is value type to be returned to another object, operator op is the function where the operator is a keyword and op is the operator to be overloaded.


1 Answers

Suppose I asked you "what is the return type of concatenation"? What would you say? Probably you'd turn around and ask "concatenation of what?" Catenation is defined on chars, strings, sequences, catenable deques, languages, discrete finite automata and a thousand other things, so the type returned is determined by the type of the arguments. But typically the type of a concatenation is the type of the arguments. Not always; the concatenation of two chars is a string, for example. But typically.

Similarly, the question "what is the type of the ! operator?" depends entirely on what is being negated, and you haven't said what you are negating. Typically the negation of a T is another T, but it need not be.

I suspect you are not asking the right question. I think the question you ought to ask is "what is a realistic scenario in which you would overload the ! operator?" The example you give from the book is terrible; it does not motivate why the author of the code is overriding the operator at all.

Here's an more realistic example. Suppose we lived in a world without nullable types. You might decide to have three-valued logic:

sealed class MyBool
{
    private MyBool() {} // No one creates it!
    public static MyBool True = new MyBool();
    public static MyBool False = new MyBool();
    public static MyBool Unknown = new MyBool();

OK, what's the rule for negation of MyBool? True becomes False, False becomes True, Unknown stays Unknown:

    public static MyBool operator !(MyBool b)
    { 
        if (b == True) return False;
        if (b == False) return True;
        return Unknown;
    }

In this scenario, the type of the ! operator is MyBool.

Of course, since C# 2.0 we have three-valued logic in the form of Nullable<bool>, but you might want more complex kinds of logic (or you might be writing C# 1.0 code).

It is harder to come up with reasonable examples of situations in which negation of a Foo results in a Bar; some sort of "monadic" workflow objects might be a possible situation -- where the negation of an object of a given type is an object of a different type that represents the deferred execution of the negation.

like image 175
Eric Lippert Avatar answered Sep 19 '22 22:09

Eric Lippert