% CPSC-KEYWORD-TEMPLATE(7) # NAME cpsc-keyword-template - define function, procedure and class templates # SYNOPSIS ```text TEMPLATE [OVERLOAD] FUNCTION name(parameters) RETURNS type statements ENDFUNCTION TEMPLATE [OVERLOAD] PROCEDURE name(parameters) statements ENDPROCEDURE TEMPLATE CLASS name fields and methods ENDCLASS ``` # DESCRIPTION TEMPLATE is a compiler extension, absent from the CIE guide. Place it before a top-level FUNCTION, PROCEDURE or CLASS, before OVERLOAD when present. Every template parameter names a type; no typename keyword is needed. Names must be distinct. A single optional type pack, written `Rest...`, must come last. Each concrete use creates and caches an ordinary function or class instance by substituting types into the template body. Function calls deduce types from parameters typed `T` or `ARRAY OF T`; bounded array parameters also require matching rank. Deduction does not look inside `Box`. Explicit arguments, as in `Max(1.5, 2.0)`, bind types positionally before deduction fills any remaining parameters. Substituted BYVAL parameters may use IMPLICIT constructors or the existing CHAR/STRING bridge; otherwise types must match exactly, including explicitly bound types: `Max(1, 2.5)` is an error. Explicit arguments are forbidden on ordinary functions and builtins. A type used only in RETURNS requires an explicit argument. A final parameter `Others : Rest...` expands into one parameter per pack type, possibly none. It cannot be an ARRAY parameter. Write `Others...` only in argument lists of function or method calls, NEW or DECLARE constructors. A bare pack value is invalid. In type argument lists, `Rest...` forwards the current type pack. Explicit pack arguments constrain a prefix; deduction can supply the remaining types. When multiple function or procedure definitions share a name, every definition, including ordinary definitions, must specify OVERLOAD. Selection prefers an exact ordinary definition, then an exact non-variadic template, then an exact variadic template. If none matches, it repeats those levels with permitted implicit conversions. Expected result types filter ordinary definitions but do not participate in template selection, which still matches substituted parameter types only. Two matches at the same level are ambiguous; no further template ordering is performed. Explicit template arguments select only templates. Normal BYREF, array-bound and access checks apply after selection; see [cpsc-keyword-overload(7)](cpsc-keyword-overload.7.md). Use a class instance such as `Box` wherever a type name is accepted, including nested instances such as `Box>`. Supply all class type arguments explicitly; variadic CLASS templates are allowed, and their methods may use the class's pack parameters. Pack fields such as `Values : Rest...` are rejected. Constructors, ownership and destruction follow ordinary CLASS rules. In particular, a declaration without constructor arguments needs a matching parameterless NEW when constructors exist; see [cpsc-keyword-class(7)](cpsc-keyword-class.7.md). Non-type parameters, SIZEOF..., member templates, operator templates, specialization and pointer-alias templates are unsupported. DEFAULT return declarations are also unsupported in templates, including methods of class templates. Within a class template, write the full instance type rather than the bare class name. Pointer-linked generic containers remain unsupported because pointer-alias templates are unavailable. Source template lists cannot be empty: `<>` is the inequality token. Nested closing `>>` works as two tokens. Separate `<` from a following `-` and `>` from a following `=` to avoid the `<-` and `>=` tokens. Expression lookahead treats `A < B, C > (D)` as a template call; parenthesize comparisons to avoid this ambiguity. # EXAMPLE ```text TEMPLATE FUNCTION Max(A : T, B : T) RETURNS T IF A > B THEN RETURN A ENDIF RETURN B ENDFUNCTION OVERLOAD PROCEDURE PrintAll() ENDPROCEDURE TEMPLATE OVERLOAD PROCEDURE PrintAll(First : T, Others : Rest...) OUTPUT First CALL PrintAll(Others...) ENDPROCEDURE TEMPLATE CLASS Box Value : T OVERLOAD PROCEDURE NEW() ENDPROCEDURE OVERLOAD PROCEDURE NEW(V : T) Value <- V ENDPROCEDURE ENDCLASS DECLARE B : Box B.Value <- Max(3, 7) OUTPUT Max(1.5, 2.0) CALL PrintAll(B.Value, 2.5, 'c', "str") ``` # SEE ALSO [cpsc-keyword-overload(7)](cpsc-keyword-overload.7.md), [cpsc-keyword-class(7)](cpsc-keyword-class.7.md), [cpsc-keyword-function(7)](cpsc-keyword-function.7.md), [cpsc-keyword-procedure(7)](cpsc-keyword-procedure.7.md), [cpsc-keyword-declare(7)](cpsc-keyword-declare.7.md), [cpsc-language-overview(7)](../cpsc-language-overview.7.md).