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
|
We've already encountered various examples of em(simple requirements): they
specify the operations that must be supported by the variable(s) declared in
the parameter lists of tt(requires) specifications. When the requirements
refer to single variables single tt(Type) parameters suffice; when
requirements involve different types then the concept's template head declares
those different types and the tt(requires) parameter list will usually define
variables of those different types. The tt(concept BasicMath) specifies two
types, and uses four simple requirements to specify the four basic arithmetic
operations:
verbinsert(-as4 examples/basicmath.cc)
Specifying constraints does not necessarily mean that the constraints as
specified literally apply to run-time situations. To require the existence of
the index operator the following simple requirement can be used:
verbinsert(-s4 //index examples/simple.cc)
Here the em(stand-in argument) 0 is used to specify the index operator's
argument. The argument value used in the simple requirement really is a
stand-in. The following code fragment compiles, as tt(string) supports the
index operator. Athough argument 0 is used in the simple requirement
specification the argument 5 is in fact being used:
verb( string str;
idx(str, 5);)
Other than tt(int) index types can be specified analogously. Here is an
example showing how to define and use a concept tt(HasStrIndex) requiring the
availability of tt(std::string) arguments of index operators:
verbinsert(-s4 //stridx examples/stridx.cc)
|