This documentation is archived and is not being maintained.

20.1 Arithmetic, Logical, and Bitwise Operators

Visual Studio .NET 2003

The common language runtime supports arithmetic operators on managed classes. They are defined as ordinary static methods with distinguished names. Most .NET compilers, including C++, map these specially named methods into infix operators for the defining class.

Example

// managed_operators.cpp
// compile with: /clr /LD
#using <mscorlib.dll>

__value class M {
   int i;
public:
   static bool op_Inequality(M m1, M m2) { // maps to operator!=
      return m1.i != m2.i;
   }
};

bool all_eq(M m1, M m2, M m3) {
   // can call op_Inequality directly
   if ( M::op_Inequality(m1, m2) )
      return false;

   // can also call M::op_Inequality using infix notation
   if ( m2 != m3 )
      return false;
   return true;
};

Constraints

  • A managed class shall not define an ordinary C++ operator, that is, by using the operator keyword.

    Example

    __gc class M {
       bool operator==(M*);   // error
    };
    __value class V {
       bool operator>=(V);    // error
    
  • A managed operator shall have static storage class.
  • A managed operator for a __value class shall specify at least one argument whose type is the defining class. The reference syntax (Section 8) is allowed for the parameters of __value class operators.
  • In a managed class, any member whose identifier is a distinguished common language runtime operator name defines a common language runtime operator. For example, the user cannot define an ordinary instance method or a data member named op_Equality.
  • A managed operator for a __gc class shall specify at least one argument whose type is pointer-to the defining class.

Example

// managed_operators2.cpp
// compile with: /clr /LD
#using <mscorlib.dll>
__gc class G {
   int i;
public:
   static bool op_Inequality(G *g1, G *g2) { return g1->i != g2->i; }
};

bool all_eq(G *g1, G *g2, G *g3) {
   if( G::op_Inequality(g1, g2) )   // direct call only
      return false;
   return true;
};

The following is the list of arithmetic, logical, and bitwise operators supported by the Common Language Specification that can be implemented using the Managed Extensions.

Unary operators

  • op_Decrement (--)
  • op_Increment (++)
  • op_Negation (!)
  • op_UnaryNegation (-)
  • op_UnaryPlus (+)

Binary operators

  • op_Addition (+)
  • op_Assign (=)
  • op_BitwiseAnd (&)
  • op_BitwiseOr (|)
  • op_Division (/)
  • op_Equality (==)
  • op_ExclusiveOr (^)
  • op_GreaterThan (>)
  • op_GreaterThanOrEqual (>=)
  • op_Inequality (!=)
  • op_LeftShift (<<)
  • op_LessThan (<)
  • op_LessThanOrEqual (<=)
  • op_LogicalAnd (&&)
  • op_LogicalOr (||)
  • op_Modulus(%)
  • op_Multiply(*)
  • op_RightShift (>>)
  • op_Subtraction (-)

Example

// managed_operators3.cpp
// compile with: /clr
#using <mscorlib.dll>

__value struct V {
   int i;
   static bool op_Equality(V v, int i) { return v.i == i; }
   static bool op_Equality(int i, V v) { return v.i == i; }
};

int main() {
   V v = {10};
   if ( v == 10 )   // calls V::op_Equality(V,int);
      return 0;
   else 
      if ( 20 == v )   // calls V::op_Equality(int,V);
         return 0;
   return 1;
}
Show: