Debian Codegraph @

yo/advancedtemplates/concepts/defining.yo

raw
1 As a prelude before actually looking at how concepts are defined it is noted
2 that concept names, like class names, type names, function names, and variable
3 names should suggest their purposes. Don't name a concept `tt(Constraint)' or
4 `tt(Concept)', but use names like `tt(Addable)' and `tt(HasValueType)'.
5
6 Concepts are templates. They start with a template header (the
7 template headers shown in the examples define a single template type
8 parameter, but multiple template parameters are also used).
9
10 In the previous section we used the concept tt(Addable). Here is how it can
11 be defined:
12 verb( template <typename Type>
13 concept Addable =
14 requires(Type lh, Type rh)
15 {
16 lh + rh;
17 };)
18
19 The concept's template header is followed by the keyword tt(concept), the
20 concept's name, and the assignment operator. Following the assignment operator
21 hi(requires)em(requirement specifications) are provided.
22
23 Semicolons end concept definitions. This concept uses a
24 em(simple requirement) (cf. section ref(REQSIMPLE)) indicating that
25 tt(operator+) must have been defined for tt(Addable) templates' types.
26
27 Requirements come in many forms. A very simple form consists of just a
28 tt(bool) value, which is sometimes useful when developing a concept. Such a
29 concept looks like this:
30 verb(template <typename Type>
31 concept IsTrue =
32 true;
33 )
34 But in most situations tt(requires) specifications are used. They resemble
35 function definitions having parameter lists optionally defining variables of
36 the types that specified in the concept's template header and compound
37 statements specifying requirements.
38
39 Concepts are em(never) instantiated. They are used compile-time to verify that
40 template types satisfy the imposed requirements. Thus there's no need
41 to use refererences in parameter lists of tt(requires) specifications. The
42 concept tt(Addable) simply uses
43 verb( requires(Type lh, Type rh))
44
45 and there's no need to specify
46 verb( requires(Type const &lh, Type const &rh))
47 (That is, usually there is no need for this. In section ref(REQNEST) we
48 encounter a situation where a more specific parameter definition might be
49 appropriate.)
50
51 Here are two examples of templates using tt(concept Addable). The first
52 example uses tt(Addable) instead of tt(typename) when specifying the template
53 header, the second example appends the concept specification to the template
54 header itself:
55 verbinsert(-s4 //requires examples/requires.cc)
56
57 Template declarations using concepts are specified accordingly. Simply replace
58 the function template's body by a semicolon.
59
60 Concepts may also be defined by extending or combining existing concepts.
61 em(Nesting) concepts is covered in section ref(REQNEST).
62
63 Although concepts are templates, they cannot be specialized. If a concept
64 should recognize specializations then these specializations must be handled by
65 the concepts' definitions. See section ref(REQCOMP) for an illustration.
66
67