Ada Reference Manual (Ada 2022)Legal Information
Contents   Index   References   Search   Previous   Next 

6.6 Overloading of Operators

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). 

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. 
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);

Contents   Index   References   Search   Previous   Next 
Ada-Europe Ada 2005 and 2012 Editions sponsored in part by Ada-Europe