12.3 Generic Instantiation
according to whether or not the
is specified. Any positional associations shall precede any named associations.
In a generic_instantiation
for a particular kind of program unit (package, procedure, or function),
denote a generic unit of the corresponding kind (generic package, generic
procedure, or generic function, respectively).
In a generic unit, Legality Rules are enforced at
compile time of the generic_declaration
and generic body, given the properties of the formals. In the visible
part and formal part of an instance, Legality Rules are enforced at compile
time of the generic_instantiation
given the properties of the actuals. In other parts of an instance, Legality
Rules are not enforced; this rule does not apply when a given rule explicitly
The instance is a copy of the text of the template.
Each use of a formal parameter becomes (in the copy) a use of the actual,
as explained below.
instance of a generic package is a package, that of a generic procedure
is a procedure, and that of a generic function is a function.
The interpretation of each construct within a generic
declaration or body is determined using the overloading rules when that
generic declaration or body is compiled. In an instance, the interpretation
of each (copied) construct is the same, except in the case of a name
that denotes the generic_declaration
or some declaration within the generic unit; the corresponding name in
the instance then denotes the corresponding copy of the denoted declaration.
The overloading rules do not apply in the instance.
In an instance, a generic_formal_parameter_declaration
declares a view whose properties are identical to those of the actual,
except when specified otherwise (in particular, see 6.1.1
“Preconditions and Postconditions
”, and 12.6
”). Similarly, for a declaration within a generic_formal_parameter_declaration
the corresponding declaration in an instance declares a view whose properties
are identical to the corresponding declaration within the declaration
of the actual.
Implicit declarations are also copied, and a name
that denotes an implicit declaration in the generic denotes the corresponding
copy in the instance. However, for a type declared within the visible
part of the generic, a whole new set of primitive subprograms is implicitly
declared for use outside the instance, and may differ from the copied
set if the properties of the type in some way depend on the properties
of some actual type specified in the instantiation. For example, if the
type in the generic is derived from a formal private type, then in the
instance the type will inherit subprograms from the corresponding actual
These new implicit declarations
occur immediately after the type declaration in the instance, and override
the copied ones. The copied ones can be called only from within the instance;
the new ones can be called only from outside the instance, although for
tagged types, the body of a new one can be executed by a call to an old
In the visible part of an instance, an explicit declaration
overrides an implicit declaration if they are homographs, as described
. On the other hand, an explicit declaration
in the private part of an instance overrides an implicit declaration
in the instance, only if the corresponding explicit declaration in the
generic overrides a corresponding implicit declaration in the generic.
Corresponding rules apply to the other kinds of overriding described
Recursive generic instantiation is not allowed in
the following sense: if a given generic unit includes an instantiation
of a second generic unit, then the instance generated by this instantiation
shall not include an instance of the first generic unit (whether this
instance is generated directly, or indirectly by intermediate instantiations).
For the elaboration of a generic_instantiation
is first evaluated. If a default is used, an implicit generic_association
is assumed for this rule. These evaluations are done in an arbitrary
order, except that the evaluation for a default actual takes place after
the evaluation for another actual if the default includes a name
that denotes the other one.
Finally, the instance
declaration and body are elaborated.
For the evaluation of a generic_association
the generic actual parameter is evaluated. Additional actions are performed
in the case of a formal object of mode in
NOTE If a formal type is not tagged,
then the type is treated as an untagged type within the generic body.
Deriving from such a type in a generic body is permitted; the new type
does not get a new tag value, even if the actual is tagged. Overriding
operations for such a derived type cannot be dispatched to from outside
Examples of generic
instantiations (see 12.1):
procedure Swap is new Exchange(Elem => Integer);
procedure Swap is new Exchange(Character); -- Swap is overloaded
function Square is new Squaring(Integer); -- "*" of Integer used by default
function Square1 is new Squaring(Item => Matrix, "*" => Matrix_Product);
function Square2 is new Squaring(Matrix, Matrix_Product); -- same as previous
package Int_Vectors is new On_Vectors(Integer, Table, "+");
Examples of uses
of instantiated units:
A := Square(A);
T : Table(1 .. 5) := (10, 20, 30, 40, 50);
N : Integer := Int_Vectors.Sigma(T); -- 150
-- (see 12.2 for the body of Sigma)
M : Integer := Sigma(T); -- 150
Ada 2005 and 2012 Editions sponsored in part by Ada-Europe