forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
concepts.dd
249 lines (214 loc) · 5.84 KB
/
concepts.dd
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
Ddoc
$(D_S Template Constraints,
$(P Templates are normally overloaded and matched based on the
template arguments being matched to the template parameters.
The template parameters can specify specializations, so that
the template argument must match particular type patterns.
Similarly, template value arguments can be constrained to
match particular types.)
$(P But this has its limitations. Many times there are
arbitrarily more
complex criteria for what should be accepted by the template.
This can be used to:
)
$(UL
$(LI more finely discriminate about which
template gets instantiated for given arguments)
$(LI provides better self-documentation about what
characteristics template parameters must have)
$(LI can provide better diagnostics when arguments don't match,
rather than an obscure error message based on the irrelevant
(to the user) internal details of the template implementation)
)
$(P Constraints address this by simply providing an
expression that must evaluate at compile time to true
after the arguments are matched to the parameters.
If it is true, then that template is a valid match for
the arguments, if not, then it is not and is passed over
during overload matching.)
$(P The constraint expression follows the template declaration
and the $(CODE if) keyword:)
---
template Foo(int N)
if (N & 1)
{
...
}
---
$(P which constrains the template $(CODE Foo) to match only if its
argument
is an odd integer. Arbitrarily complex criteria can be used, as
long as it can be computed at compile time. For example, here's
a template that only accepts prime numbers:
)
---
bool isPrime(int n)
{
if (n == 2)
return true;
if (n < 1 || (n & 1) == 0)
return false;
if (n > 3)
{
for (auto i = 3; i * i <= n; i += 2)
{
if ((n % i) == 0)
return false;
}
}
return true;
}
template Foo(int N)
if (isPrime(N))
{
...
}
Foo!(5) // ok, 5 is prime
Foo!(6) // no match for Foo
---
$(P Type constraints can be complex, too. For example, a template
Bar that will accept any floating point type using the traditional
type specializations:
)
---
template Bar(T:float)
{
...
}
template Bar(T:double)
{
...
}
template Bar(T:real)
{
...
}
---
$(P and the template implementation body must be duplicated
three times. But with constraints, this can be specified
with one template:
)
---
template Bar(T)
if (is(T == float) || is(T == double) || is(T == real))
{
...
}
---
$(P This can be simplified by using the $(CODE isFloatingPoint)
template in library module $(CODE std.traits):
)
---
import std.traits;
template Bar(T)
if (isFloatingPoint!(T))
{
...
}
---
$(P Characteristics of types can be tested, such as if
a type can be added:
)
---
// Returns true if instances of type T can be added
template isAddable(T)
{
// Works by attempting to add two instances of type T
const isAddable = __traits(compiles, (T t) { return t + t; });
}
int Foo(T)(T t)
if (isAddable!(T))
{
return 3;
}
struct S
{
void opAdd(S s) { } // an addable struct type
}
void main()
{
Foo(4); // succeeds
S s;
Foo(s); // succeeds
Foo("a"); // fails to match
}
---
$(P Since any expression that can be computed at compile time
is allowed as a constraint, constraints can be composed:
)
---
int Foo(T)(T t)
if (isAddable!(T) && isMultipliable!(T))
{
return 3;
}
---
$(P A more complex constraint can specify a list of operations
that must be doable with the type, such as $(CODE isStack) which
specifies the constraints that a stack type must have:)
----
template isStack(T)
{
const isStack =
__traits(compiles,
(T t)
{
T.value_type v = top(t);
push(t, v);
pop(t);
if (empty(t)) { }
});
}
template Foo(T)
if (isStack!(T))
{
...
}
----
$(P and constraints can deal with multiple parameters:)
---
template Foo(T, int N)
if (isAddable!(T) && isprime(N))
{
...
}
---
$(H2 Overloading based on Constraints)
$(P Given a list of overloaded templates with the same name,
constraints act as a yes/no filter to determine the list
of candidates for a match.
Overloading based on constraints can thus be achieved by
setting up constraint expressions that are mutually exclusive.
For example, overloading template $(CODE Foo) so that one
takes odd integers and the other even:
)
---
template Foo(int N) if (N & 1) { ... } // A
template Foo(int N) if (!(N & 1)) { ... } // B
...
Foo!(3) // instantiates A
Foo!(64) // instantiates B
---
$(P Constraints are not involved with determining which
template is more specialized than another.
)
---
void foo(T, int N)() if (N & 1) { ... } // A
void foo(T : int, int N)() if (N > 3) { ... } // B
...
foo!(int, 7)(); // picks B, more specialized
foo!(int, 1)(); // picks A, as it fails B's constraint
foo!("a", 7)(); // picks A
foo!("a", 4)(); // error, no match
---
$(H2 References)
$(UL
$(LI $(LINK2 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2081.pdf, Concepts (Revision 1))
by Douglas Gregor and Bjarne Stroustrup
)
)
)
Macros:
TITLE=Template Constraints
META_KEYWORDS=D Programming Language, constraints, template, concepts, C++
META_DESCRIPTION=Going beyond type patterns to constrain template instantiations.