blob: 0c03ca39ad76a78f24a41eef56057509fe4ded9a (
plain)
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
|
import core.stdc.stdio;
/*********************************************************/
template Foo(T)
{
static if (is(T : int))
alias T t1;
static if (T.sizeof == 4)
alias T t2;
static if (is(T U : int))
alias U t3;
static if (is(T* V : V*))
alias V t4;
static if (is(T W))
alias W t5;
else
alias char t5;
static if (is(T* X : X*))
{
}
}
void test1()
{
Foo!(int).t1 x1;
assert(typeid(typeof(x1)) == typeid(int));
Foo!(int).t2 x2;
assert(typeid(typeof(x2)) == typeid(int));
Foo!(int).t3 x3;
assert(typeid(typeof(x3)) == typeid(int));
Foo!(int*).t4 x4;
assert(typeid(typeof(x4)) == typeid(int*));
Foo!(int).t5 x5;
assert(typeid(typeof(x5)) == typeid(int));
Foo!(int).X x6;
assert(typeid(typeof(x6)) == typeid(int));
}
/*********************************************************/
void test2()
{
alias int T;
static if (is(T : int))
alias T t1;
static if (T.sizeof == 4)
alias T t2;
static if (is(T U : int))
alias U t3;
static if (is(T* V : V*))
alias V t4;
static if (is(T W))
alias W t5;
else
alias char t5;
static if (is(T* X : X*))
{
}
t1 x1;
assert(typeid(typeof(x1)) == typeid(int));
t2 x2;
assert(typeid(typeof(x2)) == typeid(int));
t3 x3;
assert(typeid(typeof(x3)) == typeid(int));
t4 x4;
assert(typeid(typeof(x4)) == typeid(int));
t5 x5;
assert(typeid(typeof(x5)) == typeid(int));
X x6;
assert(typeid(typeof(x6)) == typeid(int));
}
/*********************************************************/
void test3()
{
static if (is(short : int))
{ printf("1\n");
}
else
assert(0);
static if (is(short == int))
assert(0);
static if (is(int == int))
{ printf("3\n");
}
else
assert(0);
}
/*********************************************************/
template TValue(int i:1)
{
pragma(msg,"last instantiation!!!");
const int TValue = 1;
}
template TValue(int i)
{
pragma(msg,"instantiating...");
const int TValue = i * TValue!(i-1);
}
void test4()
{
assert(TValue!(3) == 6);
}
/*********************************************************/
template Reverse(string s: "") {
const char[] Reverse = "";
}
template Reverse(string s) {
const char[] Reverse = Reverse!(s[1..$]) ~ s[0];
}
void test5()
{
assert(Reverse!("Recursive string template") == "etalpmet gnirts evisruceR");
}
/*********************************************************/
template foo6(alias V)
{
int foo6()
{
return V;
}
}
class bar6(alias V)
{
int abc()
{
return V;
}
}
void test6()
{
int j = 3;
int k = 4;
int i = foo6!(j)();
i += foo6!(j)();
i += foo6!(k)();
bar6!(j) b = new bar6!(j);
i -= b.abc();
assert(i == 7);
}
/*********************************************************/
template Bind7(alias dg)
{
int Bind7()
{
dg('c');
return 0;
}
}
void test7()
{
char[] v;
void foo(char c) { v ~= c; }
alias Bind7!(foo) intv;
intv();
assert(v[0] == 'c');
}
/*********************************************************/
template sum8(real x)
{
static if (x <= 1.0L){
const real sum8 = x;
}else{
const real sum8 = x + sum8!(x - 1.0L);
}
}
void test8()
{
real x = sum8!(3.0L);
if(x != 6.0L){
assert(0);
}
}
/*********************************************************/
int main()
{
test1();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
printf("Success\n");
return 0;
}
|