Debian Codegraph @

yo/advancedtemplates/concepts/intro.yo

raw
1 COMMENT(See intro.txt for stuff ISN)
2
3 bf(C++) is a strongly typed language: a function tt(add(int lhs, int rhs))
4 doesn't accept tt(std::string) arguments, even though the actual operations
5 (tt(lhs + rhs)) are identical for tt(ints) and tt(strings).
6
7 Templates were introduced so we could design em(recipes) for the compiler,
8 allowing it to construct type-safe overloaded versions of functions and
9 classes while keeping their type-safety.
10
11 A basic addition function template adding two values looks like this:
12 verb( template <typename Type>
13 Type add(Type const &lhs, Type const &rhs)
14 {
15 return lhs + rhs;
16 }
17 )
18
19 When this function template is called with arguments of types that do not
20 support tt(operator+) then the compiler notices this, and it will generate an
21 error. E.g., when calling
22 verb( add(std::cerr, std::cout);)
23 the tt(g++) compiler produces some 140 lines of error messages. It notices
24 that there's no tt(operator+) for tt(std::ostream) objects, and then tells us
25 what else we might have done (like adding two tt(ints)), and where the
26 construction of the tt(add) function that should accept tt(std::ostream)
27 arguments went wrong. In fact, 140 lines of error messages is rather benign.
28 Getting several hundreds of lines is quite common, and sometimes the
29 location of the error isn't mentioned at the top but somewhere near the end of
30 the error message output.
31
32 The C2a standard introduced hi(concept) em(concepts) allowing us to specify
33 em(requirements) for template types. When applying an appropriate concept to
34 the definition of the tt(add) function template the compiler immediately
35 pinpoints the error, telling us where and why the error occurred in some 15
36 instead of 140 lines of error messages.
37
38 The reduction of the number of lines of error messages by itself is a
39 boon. But the fact that concepts allow us to consciously develop our
40 templates, realizing what the precise requirements are for their use, is at
41 least as important: it improves the template's documentation, and thus our
42 understanding of templates.
43
44 Concepts may be considered the template's answer to the philosphy that lies
45 behind a strongly typed language. By applying concepts to templates we can
46 specify type-requirements rather than using the traditional `shotgun
47 empiricism' approach where templates are bluntly used, knowing that the
48 compiler will complain if things are incorrect. In that sense concepts provide
49 type definitions of types. Concepts have names and can (among other) be used
50 in template headers where the concept names replace the traditional
51 tt(typename) keywords.
52
53 As an opening illustration, assume that a concept tt(Addable) exists
54 specifying that tt(operator+) must have been defined for the template's
55 type. The above function template tt(add) can now be formulated as:
56 verb( template<Addable Type>
57 Type add(Type const &lhs, Type const &rhs)
58 {
59 return lhs + rhs;
60 })
61
62 From now on every type that is actually passed to tt(add) must be
63 satisfy the tt(Addable) requirements. Here are two expressions
64 using tt(add):
65 verb( add("first"s, "second"s); // (1)
66 add(map<int, int>{}, map<int, int>{}); // (2))
67
68 Expression (1) flawlessly compiles as tt(string) objects can be added;
69 expression (2) fails with the compiler reporting something like
70 verb( error: use of function `Type add(const Type&, const Type&)
71 [with Type = std::unordered_map<int, int>]' with unsatisfied constraints
72 add(unordered_map<int, int>{}, unordered_map<int, int>{});
73 note: constraints not satisfied
74
75 Type add(const Type&, const Type&)
76 ...
77 note: the required expression `(lh + rh)' is invalid)
78
79 COMMENT(examples/intro1.cc)
80
81 The error message's final `note' clearly states the cause of the problem:
82 you can't add maps.
83
84 The difference between the compiler's report using concepts and not using
85 concepts again is impressive. When using the traditional tt(typename Type)
86 specification in the template header the compiler produces some 17 kB of error
87 messages, spread out over more than 200 lines.
88
89 In the following sections we cover how concepts are defined, what kind of
90 requirements can be formulated, and how they can be used in practice.