The declaration of a function whose designator is an operator symbol is used to overload an operator. The sequence of characters of the operator symbol must be either a logical, a relational, a binary adding, a unary adding, a multiplying, or a highest precedence operator (see 4.5). Neither membership tests nor the short-circuit control forms are allowed as function designators.
The subprogram specification of a unary operator must have a single parameter. The subprogram specification of a binary operator must have two parameters; for each use of this operator, the first parameter takes the left operand as actual parameter, the second parameter takes the right operand. Similarly, 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. 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).
For each of the operators "+" and "-", overloading is allowed both as a unary and as a binary operator.
The explicit declaration of a function that overloads the equality operator "=", other than by a renaming declaration, is only allowed if both parameters are of the same limited type. An overloading of equality must deliver a result of the predefined type BOOLEAN; it also implicitly overloads the inequality operator "/=" so that this still gives the complementary result to the equality operator. Explicit overloading of the inequality operator is not allowed.
A renaming declaration whose designator is the equality operator is only allowed to rename another equality operator. (For example, such a renaming declaration can be used when equality is visible by selection but not directly visible.)
Overloading of relational operators does not affect basic comparisons such as testing for membership in a range or the choices in a case statement.
function "+" (LEFT, RIGHT : MATRIX) return MATRIX; function "+" (LEFT, RIGHT : VECTOR) return VECTOR; -- assuming that A, B, and C are of the type VECTOR -- the three following assignments are equivalent A := B + C; A := "+"(B, C); A := "+"(LEFT => B, RIGHT => C);
References: allow, actual parameter, binary adding operator, and 4.5.3, boolean predefined type, character, complementary result, declaration, default expression for a formal parameter, designator, directly visible, equality operator, formal parameter, function declaration, highest precedence operator, and 4.5.6, implicit declaration, inequality operator, limited type, logical operator, and 4.5.1, membership test, and 4.5.2, multiplying operator, and 4.5.5, operator, operator symbol, overloading, and 8.7, relational operator, and 4.5.2, short-circuit control form, and 4.5.1, type definition, unary adding operator, and 4.5.4, visible by selection.
Rationale references: 11.4.1 Overloading of Operators
Style Guide references: 5.7.5 Overloading the Equality Operator
Address any questions or comments to email@example.com.