Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify whether a type overloads/supports a certain operator?

How can I check whether a certain type implements a certain operator?

struct CustomOperatorsClass
{
    public int Value { get; private set; }


    public CustomOperatorsClass( int value )
        : this()
    {
        Value = value;
    }

    static public CustomOperatorsClass operator +(
        CustomOperatorsClass a, CustomOperatorsClass b )
    {
        return new CustomOperatorsClass( a.Value + b.Value );
    }
}

Following two checks should return true:

typeof( CustomOperatorsClass ).HasOperator( Operator.Addition )
typeof( int ).HasOperator( Operator.Addition )
like image 910
Steven Jeuris Avatar asked Dec 15 '11 16:12

Steven Jeuris


People also ask

Which operators Cannot be overloaded?

The operator that cannot be overloaded in C language is the Scope resolution operator (::).

Can we overload () operator?

You can only overload existing operators. You can't overload new operators. Some operators cannot be overloaded using a friend function. However, such operators can be overloaded using member function.

Can we overload == operator in C ++?

So operator overloading lets us define the meaning of an existing operator (note that you cannot overload some operators) for the operands of a user defined type (for example, a class is a user defined type).

Which operators can be overloaded?

The two member access operators, operator->() and operator->*() can be overloaded. The most common use of overloading these operators is with defining expression template classes, which is not a common programming technique.


1 Answers

You should check if class have method with op_Addition name You can find overloaded method names here,
Hope this helps

like image 105
Arsen Mkrtchyan Avatar answered Sep 16 '22 15:09

Arsen Mkrtchyan