Skip to main content

6.6 Overloading of Operators

danger

This Reference Manual output has not been verified, and may contain omissions or errors. Report any problems on the tracking issue

1

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/5

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

2.b/3
discussion

This equivalence extends to uses of function_call in most other language rules. However, as often happens, the equivalence is not perfect, as operator calls are not a name, while a function_call is a name. Thus, operator calls cannot be used in contexts that require a name (such as a rename of an object). A direct fix for this problem would be very disruptive, and thus we have not done that. However, qualifying an operator call can be used as a workaround in contexts that require a name.

Legality Rules

3/3

The subprogram_specification of a unary or binary operator shall have one or two parameters, respectively. The parameters shall be of mode in. 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, and they are all of mode in.

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/3

An explicit declaration of "=" whose result type is Boolean implicitly declares an operator "/=" that gives the complementary result.

6.a/3
discussion

A "/=" defined by this rule is considered user-defined, which means that it will be inherited by a derived type. “User-defined” means “not language-defined” for the purposes of inheritance, that is anything other than predefined operators.

7/5

NOTE The operators "+" and "–" are both unary and binary operators, and hence can 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 C are of the type Vector -- the following two statements are equivalent: A := B + C; A := "+"(B, C);

Extensions to Ada 83

9.a

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.

Wording Changes from Ada 2005

9.c/3
correction

Corrected the wording so that only explicit declarations of "=" cause an implicit declaration of "/="; otherwise, we could get multiple implicit definitions of "/=" without an obvious way to chose between them.

9.d/3

Added wording so that operators only allow parameters of mode in. This was made necessary by the elimination elsewhere of the restriction that function parameters be only of mode in.