| 1 |
Concepts can be nested. Being able to nest concepts is very useful as it |
| 2 |
allows us to hierarchically order concepts and to define concepts in terms of |
| 3 |
existing concepts. |
| 4 |
|
| 5 |
In chapter ref(STL) iterators were introduced (section |
| 6 |
ref(ITERATORS)). Commonly five conceptually different iterator types are |
| 7 |
distinguished: |
| 8 |
itemization( |
| 9 |
it() Input iterators are incrementable, and they support dereferencing to |
| 10 |
const values; |
| 11 |
it() Output iterators are like Input iterators, but they refer to |
| 12 |
non-const values; |
| 13 |
it() Forward iterators combine Input and Output iterators; |
| 14 |
it() Bidirectional iterators are like Forward iterators, but |
| 15 |
they also support decrement operators; |
| 16 |
it() RandomAccess iterators are like Bidirectional iterators, but these |
| 17 |
iterators also support addition and subtraction by any stepsize. |
| 18 |
) |
| 19 |
|
| 20 |
figure(advancedtemplates/concepts/nested)(Concept Hierarchy)(ConceptFig) |
| 21 |
|
| 22 |
All iterator types support (in)equality checks and increment operators. Thus, |
| 23 |
at the basis of all iterators we find the requirements that iterators must be |
| 24 |
comparable and incrementable. Concepts covering those requirements are easily |
| 25 |
constructed (see also figure ref(ConceptFig)): |
| 26 |
verbinsert(-s4 //comparable examples/nested.cc) |
| 27 |
Note that no type is specified following the tt(lhs == rhs) and tt(lhs != |
| 28 |
rhs) requirements, as those types are implied by their operators. |
| 29 |
|
| 30 |
Two more concepts are defined: one allowing dereferencing pointers |
| 31 |
returning constant references and one returning modifiable references. To |
| 32 |
allow the compiler to verify those requirements we also implicitly require the |
| 33 |
(commonly encountered) existence of typename tt(Type::value_type): |
| 34 |
verbinsert(-s4 //dereference examples/nested.cc) |
| 35 |
verbinsert(-as4 examples/constderef.cc) |
| 36 |
Not much of a hierarchy so far, but that changes now that we're about to |
| 37 |
define concepts for iterators. |
| 38 |
|
| 39 |
An input iterator is an iterator that is comparable, incrementable and |
| 40 |
const-dereferenceable. For each of these requirements concepts were defined |
| 41 |
which can be combined using boolean operators when defining the concept |
| 42 |
tt(InIterator). Note that template type parameters of concepts em(must) use |
| 43 |
the tt(typename) keyword. Concepts' |
| 44 |
hi(concept: constraints) template parameters cannot be constrained by |
| 45 |
specifying them in terms of existing concepts (which em(is) possible when |
| 46 |
defining function and class templates). |
| 47 |
|
| 48 |
Here is the definition of the concept tt(InIterator). The function template |
| 49 |
tt(inFun) (below the concept tt(InIterator)) illustrates how a |
| 50 |
constrained template parameter type can be specified in template headers: |
| 51 |
verbinsert(-s4 //initerator examples/nested.cc) |
| 52 |
|
| 53 |
The concept for output iterators (and its use, as in the function |
| 54 |
template tt(outFun)) is defined analogously. This time requiring |
| 55 |
dereferenceable types rather than const-dereferenceable types: |
| 56 |
verbinsert(-s4 //outiterator examples/nested.cc) |
| 57 |
|
| 58 |
For forward iterators the concept tt(FwdIterator) is defined. A forward |
| 59 |
iterator combines the characteristics of input and output iterators, and we |
| 60 |
may want to define a forward iterator by requiring the requirements of the |
| 61 |
tt(InIterator) and tt(OutIterator) concepts. |
| 62 |
|
| 63 |
However, there's a slight problem. The following class (struct) defines const |
| 64 |
and non-const dereference operators and may be therefore be passed to |
| 65 |
functions expecting input or output iterators: |
| 66 |
verbinsert(-s4 //iterable examples/nested.cc) |
| 67 |
|
| 68 |
But when a function template requires tt(ConstDerefenceable) arguments then |
| 69 |
the compiler notices that the overloaded member tt(int &operator*()) doesn't |
| 70 |
return an tt(int const &). Even though tt(int const &operator*() const) is |
| 71 |
available compilation fails. This problem can be solved in two ways: noting |
| 72 |
that an tt(int &) can be converted to an tt(int const &) the predefined |
| 73 |
concept tt(std::convertible_to) instead of tt(std::same_as) can be used in |
| 74 |
tt(ConstDereferenceable); alternatively its tt(requires) clause can specify |
| 75 |
tt(Type const &type) instead of just tt(Type type). Here is a definition of |
| 76 |
tt(ConstDereferenceable) that, when defining the concept tt(FwdIter), can be |
| 77 |
used in combination with tt(Dereferenceable): |
| 78 |
verbinsert(-s4 //constderef examples/nested.cc) |
| 79 |
COMMENT( |
| 80 |
verbinsert(-s4 //fwditerator examples/nested.cc) |
| 81 |
END) |
| 82 |
|
| 83 |
The final two iterator types pose no problems: the concept tt(BiIterator) |
| 84 |
requires the constraints of the concept tt(FwdIterator) as well as decrement |
| 85 |
operators, and finally the concept tt(RndIterator) requires the constraints of |
| 86 |
tt(BiIterator) and in addition iterator increments decrements for any step |
| 87 |
size as well as the possibility to subtract iterators: |
| 88 |
verbinsert(-s4 //bidir examples/nested.cc) |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|