1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
Templates are recipes used by the compiler to construct code tailored to
specific types. Since they're recipes they cannot be compiled to running
code. Traditionally templates are implemented in header files, and when used
the compiler instantiates templates with the correct types using the
templates' recipes.
Since modules are defined in source files there are no header files anymore
when using modules. Modules specify their components in source files,
replacing the traditional header files: by defining templates in a module
interface unit the template's recipe character is kept, and they are
instantiated when needed.
Here's an initial example. A tt(module Adder) exports a function template
tt(add) adding two values and returning their sum:
verbinclude(-as4 examples/template/template/modadder.cc)
Source files importing tt(Adder) can now add values of types supporting the
tt(+) operator:
verbinclude(-as4 examples/template/main.cc)
producing the following output:
verb( 3
3.3
hello world
)
|