Go to the first, previous, next, last section, table of contents.


8 Template Specifications

A C++ template specifies a family of related classes or functions (see Chapter 8 [Stroustrup91]). One way to think of a template is as a fancy macro for generating classes or functions. For example, in C++ one can have a template class Set, and use it to generate the types Stack<int>, Stack<float>, Stack<Stack<String>>, and so on. The types int, float, and Stack<String> in this example are called the template arguments. The types Stack<int> and so on are said to be instantiations of the template Set. The template Stack is specified in terms of a formal (i.e., dummy) argument type.

In Larch/C++ templates usually also take traits as arguments [Edwards-etal94] [Ernst-etal91] [Goguen84]. Hence the above instantiations would look like Stack<int /*@ using int @*/>, Stack<float /*@ using float @*/>, etc.

Larch/C++ also includes a way to specify the interface and behavior required of a type parameter. This is done using a where-seq.

The syntax of template-declarations is very the same as the proposed C++ standard [Stroustrup95], except that Larch/C++ adds to the C++ syntax. One addition is the expects-clause, which is used to specify trait parameters. Another is the where-seq, which specifies the required capabilities of type parameters. See section 5 Declarations for the syntax of init-declarator. See section 6.13 Specifying Higher-Order Functions for the syntax of expects-clause.

template-declaration ::= [ export ] template < template-parameter-list [ expects-clause ] > 
[ where-seq ] declaration
template-parameter-list ::= template-parameter [ , template-parameter ] ...
template-parameter ::= type-parameter | parameter-declaration
type-parameter ::= class [ identifier ] [ type-init ] | typename [ identifier ] [ type-init ]
        | template < template-parameter-list [ expects-clause ] > class [ identifier ] [ template-init ]
type-init ::= = type-id
template-init ::= = complete-template-class-name | = complete-template-non-class-name
complete-template-non-class-name ::= [ :: ] [ nested-name-specifier ] template-non-class-name

The declaration should declare or specify a class or function; that is only class or function template specification are allowed (see Section r.14 of [Stroustrup91]).

There are two kinds of template-parameters. The most common is a type-parameter, which usually has the form class identifier. In this form, the identifier is treated as a type name in the body of the template class or function being declared. The C++ syntax uses class for declaring such formal type parameters, but that does not mean that the actual template argument has to be the type of a class: it can be any type that satisfies the requirements placed on the identifier in the where-seq.

A template can also be declared as a template argument. This is new syntax introduced in the coming C++ standard [Stroustrup95] [ANSI95].

The second form of a template-parameter allows constant expressions to be passed to templates (see Chapter 8 [Stroustrup91]). See section 5.2 Declaration Specifiers for the syntax of decl-specifier. See section 5.4 Declarators for the syntax of a declarator. See section 5.1 Initializers for the syntax of an initializer.


Go to the first, previous, next, last section, table of contents.