6.6 Overloading of Operators
This Reference Manual output has not been verified, and may contain omissions or errors. Report any problems on the tracking issue
An operator is a function whose designator
is an operator_symbol
. [Operators, like other functions, may be overloaded.]
Name Resolution Rules
2Each 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).
logical_operator
, relational_operator
, binary_adding_operator
, unary_adding_operator
, multiplying_operator
, and highest_precedence_operator
. 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/3The 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.
Default_expression
s are not allowed for the parameters of an operator (whether the operator is declared with an explicit subprogram_specification
or by a generic_instantiation
).
An explicit declaration of "/=" shall not have a result type of the predefined type Boolean.
Static Semantics
6/3An explicit declaration of "=" whose result type is Boolean implicitly declares an operator "/=" that gives the complementary result.
Examples
8Examples of user-defined operators:
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);