File: intro.yo

package info (click to toggle)
c%2B%2B-annotations 11.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 11,244 kB
  • sloc: cpp: 21,698; makefile: 1,505; ansic: 165; sh: 121; perl: 90
file content (41 lines) | stat: -rw-r--r-- 2,157 bytes parent folder | download | duplicates (4)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
The main purpose of templates is to provide a generic definition of classes
and functions that may then be tailored to specific types.

But templates allow us to do more than that. If not for compiler
implementation limitations, templates could be used to program, at
compile-time, just about anything we use computers for. This remarkable feat,
offered by no other current-day computer language, stems from the fact that
templates allow us to do three things at compile-time:
    itemization(
    it() Templates allow us to do integer arithmetic (and to save computed
values symbolically);
    it() Templates allow us to make compile-time decisions;
    it() Templates allow us to do things repeatedly.
    )

Of course, asking the compiler to compute, e.g., prime numbers, is one
thing. But it's a completely different thing to do so in an award winning
way. Don't expect speed records to be broken when the compiler performs
complex calculations for us. But that's all beside the point. In the end we
em(can) ask the compiler to compute virtually anything using bf(C++)'s
template language, including prime numbers....

In this chapter these remarkable features of templates are
discussed. Following a short overview of subtleties related to templates the
main characteristics of i(template meta programming) are introduced.

In addition to template type and template non-type parameters there is a third
kind of template parameter, the
    emi(template template parameter). This kind of template parameter is
introduced shortly, laying the groundwork for the discussion of em(trait
classes) and em(policy classes).

This chapter ends with the discussion of several additional and interesting
applications of templates: adapting compiler error messages, conversions to
class types and an elaborate example discussing compile-time list processing.

Much of the inspiration for this chapter came from two highly recommended
books: Andrei Alexandrescu's 2001 book
    hi(Alexandrescu, A.)bf(Modern C++ design) (Addison-Wesley) and Nicolai
Josutis and David mbox(Vandevoorde's) 2003 book
    hi(Josutis, N.)hi(Vandevoorde, D.)bf(Templates) (Addison-Wesley).