To: J3 J3/24-126 From: generics Subject: Formal syntax (2 of 3): templates and instantiation Date: 2024-June-10 Reference: 24-125.txt This paper is the second of three that comprise the formal syntax for generic programming features in Fortran, aka templates. 1. Introduction =============== This paper defines the syntax for the TEMPLATE construct, the INSTANTIATE statement, simple template procedures, and inline instantiation. Paper 1 covers the syntax for deferred arguments, and paper 3 covers the syntax for the REQUIREMENT construct and REQUIRES statement. Section 2 provides examples exercising the proposed syntax. Section 3 provides the syntax for the TEMPLATE construct and simple template procedures. Section 4 provides syntax for the INSTANTIATE statement and inline instantiation of simple template procedures. 2. Illustrative examples ======================== An example template is shown below. TEMPLATE TMPL{T, F} TYPE, DEFERRED :: T INTERFACE FUNCTION F(X) TYPE(T), INTENT(IN) :: X TYPE(T) :: F END FUNCTION END INTERFACE CONTAINS SUBROUTINE S(Y) TYPE(T), INTENT(INOUT) :: Y Y = F(Y) END SUBROUTINE END TEMPLATE An example simple template procedure, equivalent to the above template is shown below. SUBROUTINE S{T, F}(Y) TYPE, DEFERRED :: T INTERFACE FUNCTION F(X) TYPE(T), INTENT(IN) :: X TYPE(T) :: F END FUNCTION END INTERFACE TYPE(T), INTENT(INOUT) :: Y Y = F(Y) END SUBROUTINE Example instantiate statements of TMPL are: INSTANTIATE TMPL{integer, operator(-)}, only: negate => S INSTANTIATE TMPL{real, sin} An example of an inline instantiation of the simple template procedure would be as follows. CALL S{integer, operator(-)}(i) CALL S{real, sin}(x) 3. Syntax for templates and simple template procedures ====================================================== 3.1 Syntax for the TEMPLATE construct ------------------------------------- A template is a set of declarations, specifications and definitions that are enabled by instantiation. A TEMPLATE construct defines a template. A template is a scoping unit to which use, host, and deferred argument association can be applied. A template can be defined in the specification section of a program unit other than a block data program unit.