Debian Codegraph @

yo/advancedtemplates/concepts/twoparams.yo

raw
1 The following concepts define two template type parameters. Their generic
2 form is
3 verb( template <typename LHS, typename RHS>
4 concept Name =
5 ... requirements ...
6 ;)
7 When used in compound requirements the compiler deduces the type of the
8 compound expression, and then uses that type as LHS. In the type requirement
9 following the compound statement only the RHS type is specified. For example
10 (using the concept tt(std::same_as) (see below)), to require that a function
11 tt(fun), receiving an argument of some type tt(Type), returns a
12 tt(std::string) the following concept can be defined:
13 verb(template<typename Type>
14 concept StringFun =
15 requires(Type param)
16 {
17 { fun(param) } -> std::same_as<std::string>;
18 };)
19
20 itemization(
21 ittq(assignable_from)(requires that RHS-typed expressions
22 can be assigned to LHS-typed expressions;)
23
24 ittq(common_reference_with)(requires that both types can be converted to an
25 identical (reference) type. The concept is satisfied for two identical
26 types, but also for two types where one of them is derived from the
27 other. E.g.,
28 verbinclude(//commonref examples/commonref.cc))
29
30 ittq(common_with)(works like the previous concept;)
31
32 ittq(convertible_to)(requires that the LHS type can automatically be converted
33 to the RHS type:
34 verbinsert(//convertible examples/convertibleto.cc))
35
36 ittq(derived_from)(requires that the LHS type is derived from the RHS type;)
37
38 ittq(equality_comparable_with)(requires that operators tt(operator==) and
39 tt(operator!=) are available to compare LHS- and RHS-type variables (in
40 any order);)
41
42 ittq(same_as)(requires that the LHS type is the same as the RHS type. Note
43 that this concept acts rather strictly. For example, tt(std::same_as<long
44 int, int>) does not satisfy the requirement. If such a strict equality
45 isn't really required then tt(convertible_to) might offer a workable
46 alternative;)
47
48 ittq(swappable_with)(requires that two objects of possibly different types can
49 be swapped. The more restrictive variant requiring objects of identical
50 types is named tt(swappable);)
51
52 ittq(totally_ordered_with)(requires that two objects of possibly different
53 types can be ordered using the operators tt(==, !=, <, <=, >,) and
54 tt(>=). The requirements for ordering is hi(ordering: strict)em(strict):
55 for any two objects tt(one) and tt(two) either tt(one < two, one == two,)
56 or tt(one > two) is true. The more restrictive variant requiring objects
57 of identical types is named tt(totally_ordered).)
58 )