cppannotations/annotations/yo/functiontemplates/nameresolution.yo
Frank B. Brokken 777b182edd Moved all files but 'excluded', 'sf', and 'sourcetar' to ./annotations
This allowed me to standardize the sourcetar and sf/* scripts: the base
    directory (containing ./git) is now empty, except for maintenance scripts,
    while the source files and build scripts of the annotations are stored in
    a subdirectory of their own.
2013-05-29 20:44:08 +02:00

53 lines
2.8 KiB
Text

Consider this definition of the tt(add) function template:
verb(
template <typename Container, typename Type>
Type add(Container const &container, Type init)
{
return std::accumulate(container.begin(), container.end(), init);
}
)
Here tt(std::accumulate) is called using tt(container)'s tt(begin) and
tt(end) members.
The calls tt(container.begin()) and tt(container.end()) are said to
hi(template: statements depending on type parameters)
em(depend on template type parameters). The compiler, not having seen
tt(container)'s interface, cannot check whether tt(container) actually
has members tt(begin) and tt(end) returning input iterators.
On the other hand, tt(std::accumulate) itself is independent of any
template type parameter. Its em(arguments) depend on template parameters, but
the function call itself isn't. Statements in a template's body that are
independent of template type parameters are said em(not to depend on template
type parameters).
When the compiler encounters a template definition, it verifies the
syntactic correctness of all statements not depending on template
parameters. I.e., it must have seen all class definitions, all type
definitions, all function declarations etc. that are used in those statements.
If the compiler hasn't seen the required definitions and declarations then it
will reject the template's definition. Therefore, when submitting the above
template to the compiler the header file tt(numeric) must first have been
included as this header file declares tt(std::accumulate).
With statements depending on template parameters the compiler cannot
perform those extensive syntactic checks. It has no way to verify the
existence of a member tt(begin) for the as yet unspecified type
tt(Container). In these cases the compiler performs superficial checks,
assuming that the required members, operators and types eventually become
available.
The location in the program's source where the template is instantiated is
hi(template: point of instantiation) hi(point of instantiation) called
its em(point of instantiation). At the point of instantiation the compiler
deduces the actual types of the template's parameters. At that point it checks
the syntactic correctness of the template's statements that depend on template
type parameters. This implies that the compiler must have seen the required
declarations em(only at the point of instantiation). As a
i(rule of thumb),
you should make sure that all required declarations (usually: header
files) have been read by the compiler at every point of instantiation of the
template. For the template's definition itself a more relaxed requirement can
be formulated. When the definition is read only the declarations required for
statements em(not) depending on the template's type parameters must have been
provided.