Next: , Previous: 6.5.1, Up: 6


6.6 Overloading of Operators

1
{operator} {user−defined operator} {operator (user−defined)} An operator is a function whose designator is an operator_symbol. [Operators, like other functions, may be overloaded.]

Name Resolution Rules

2
Each use of a unary or binary operator is equivalent to a function_call with function_prefix being the corresponding operator_symbol, and with (respectively) one or two positional actual parameters being the operand(s) of the operator (in order).

2.a

To be honest: We also use the term operator (in Section 4 and in 6.1) to refer to one of the syntactic categories defined in 4.5, "4.5 Operators and Expression Evaluation" whose names end with "_operator:" logical_operator (see 4.5), relational_operator (see 4.5), binary_adding_operator (see 4.5), unary_adding_operator (see 4.5), multiplying_operator (see 4.5), and highest_precedence_operator (see 4.5).
Legality Rules

3
The subprogram_specification of a unary or binary operator shall have one or two parameters, respectively. A generic function instantiation whose designator is an operator_symbol is only allowed if the specification of the generic function has the corresponding number of parameters.

4
Default_expressions are not allowed for the parameters of an operator (whether the operator is declared with an explicit subprogram_specification or by a generic_instantiation).

5
An explicit declaration of "/=" shall not have a result type of the predefined type Boolean.

Static Semantics

6
A declaration of "=" whose result type is Boolean implicitly declares a declaration of "/=" that gives the complementary result.

     NOTES

7

 The operators "+" and "−−" are both unary and binary operators, and hence may be overloaded with both one− and two−parameter functions.
Examples

8
Examples of user−defined operators:

9

     function "+" (Left, Right Matrix) return Matrix;
     function "+" (Left, Right Vector) return Vector;
     
     −−  assuming that A, B, and are of the type Vector
     −−  the following two statements are equivalent:
     
     A := C;
     A := "+"(B, C);
Extensions to Ada 83

9.a

{extensions to Ada 83} Explicit declarations of "=" are now permitted for any combination of parameter and result types.

9.b

Explicit declarations of "/=" are now permitted, so long as the result type is not Boolean.