[Home] [Prev] [Next] [Index]

12.2 Generic Bodies

12.2 Generic Bodies

1
The body of a generic subprogram or generic package is a template for the bodies of the corresponding subprograms or packages obtained by generic instantiations. The syntax of a generic body is identical to that of a nongeneric body.

2
For each declaration of a generic subprogram, there must be a corresponding body.

3
The elaboration of a generic body has no other effect than to establish that the body can from then on be used as the template for obtaining the corresponding instances.

4
Example of a generic procedure body:

procedure EXCHANGE(U, V : in out ELEM) is -- see example in 12.1
     T : ELEM; -- the generic formal type
begin
    T := U;
    U := V;
    V := T;
end EXCHANGE;

5
Example of a generic function body:

function SQUARING(X : ITEM) return ITEM is -- see example in 12.1
begin
    return X*X; -- the formal operator "*"
end;

6
Example of a generic package body:

package body ON_VECTORS is -- see example in 12.1

    function SUM(A, B : VECTOR) return VECTOR is
         RESULT : VECTOR(A'RANGE);                  -- the formal type VECTOR
         BIAS : constant INTEGER := B'FIRST - A'FIRST;
    begin
         if A'LENGTH /= B'LENGTH then
            raise LENGTH_ERROR;
         end if;

         for N in A'RANGE loop
              RESULT(N) := SUM(A(N), B(N + BIAS));      -- the formal
                     -- function SUM
        end loop;
        return RESULT;
    end;

    function SIGMA(A : VECTOR) return ITEM is
         TOTAL : ITEM := A(A'FIRST); -- the formal type ITEM
    begin
        for N in A'FIRST + 1 .. A'LAST loop
            TOTAL := SUM(TOTAL, A(N)); -- the formal function SUM
        end loop;
        return TOTAL;
    end;
end;

7
References:

*
body 3.9

*
elaboration 3.9

*
generic body 12.1

*
generic instantiation 12.3

*
generic package 12.1

*
generic subprogram 12.1

*
instance 12.3

*
package body 7.1

*
package 7

*
subprogram 6

*
subprogram body 6.3



[Home] [Prev] [Next] [Index]

documentation@rational.com
Copyright © 1993-2000, Rational Software Corporation. All rights reserved.