Debian Codegraph @

yo/advancedtemplates/concepts/declarations.yo

raw
1 Constrained function- or class-templates can be declared as usual:
2 instead of the implementations semicolons are used. When declaring a function-
3 or class-template without constraint specifications then the function or class
4 template is unconstrained and won't match existing constrained overloaded
5 versions of such function or class templates. On the other hand,
6 hi(concept: declaration) concepts can+em(not) be declared. So if a concept
7 definition must be used in multiple source or header files then the concept
8 definition normally is provided in a header file of its own which is then
9 included by files using the concept.
10
11 Here are some simple examples illustrating how constrained
12 function templates are declared:
13 verbinsert(-s4 //functions examples/declarations.cc)
14
15 When declaring class templates their requires-clauses must precede the class
16 names. Also, when unconstrained class templates are available the constrained
17 class templates are in fact specializations and must be declared accordingly:
18 verbinsert(-s4 //classes examples/declarations.cc)
19
20 Multiple constraints can also be declared:
21 verbinsert(-s4 //multi examples/declarations.cc)
22
23 Although specializations may define different constraints (e.g., there may
24 also be a concept tt(Subtractable)), a tt(Data) specialization for
25 subtractable types might also be defined:
26 verb( template <Subtractable Type>
27 struct Data<Type>
28 {};)
29
30 But this is probably not what you want: when defining tt(Data<vector<int>>{}),
31 where tt(template<typename Type> Data) is merely declared, the compiler
32 complains about an em(incomplete type `struct Data<std::vector<int>>') as it
33 cannot use the specialization for either tt(Addable) or tt(Subtractable). So
34 it falls back on the generic template, but for that one no implementation is
35 available, and hence it's incomplete.
36
37 Defining a template requiring two types, the first being tt(Addable) and the
38 second template argument being unrestricted, while a specialization is defined
39 requiring a tt(Subtractable) type and an tt(int), then that also
40 does'n work as intended. In that case, the templates might be:
41 verbinsert(-s4 //two examples/multideclare.cc)
42
43 Here, if the first template argument isn't a subtractable type (like a
44 tt(vector<int>)), and the second argument em(is) an tt(int) then the compiler
45 simply won't use it because the 1st argument isn't a subtractable type.
46
47 Therefore it falls back to the first (generic) template definition. However,
48 that one doesn't work either, because the first argument also isn't
49 addable, and you receive complaints about tt((lh + rh)) being ill-formed.
50
51 Now, as you specified tt(int) as the template's second argument chances are
52 that you expected a complaint about tt((lh - rh)) being ill formed, but that
53 doesn't happen. In other words: using concepts still requires you to
54 understand what's going on. Concepts help the compiler to pinpoint reasons
55 for compilation failures, but in the end it's you who has to understand what
56 you're doing in order to grasp what the compiler is trying to tell you.