1 /* 2 TEST_OUTPUT: 3 --- 4 instantiating... 5 instantiating... 6 last instantiation!!! 7 --- 8 9 RUN_OUTPUT: 10 --- 11 1 12 3 13 Success 14 --- 15 */ 16 17 import core.stdc.stdio; 18 19 /*********************************************************/ 20 Foo(T)21template Foo(T) 22 { 23 static if (is(T : int)) 24 alias T t1; 25 26 static if (T.sizeof == 4) 27 alias T t2; 28 29 static if (is(T U : int)) 30 alias U t3; 31 32 static if (is(T* V : V*)) 33 alias V t4; 34 35 static if (is(T W)) 36 alias W t5; 37 else 38 alias char t5; 39 40 static if (is(T* X : X*)) 41 { 42 } 43 } 44 test1()45void test1() 46 { 47 Foo!(int).t1 x1; 48 assert(typeid(typeof(x1)) == typeid(int)); 49 50 Foo!(int).t2 x2; 51 assert(typeid(typeof(x2)) == typeid(int)); 52 53 Foo!(int).t3 x3; 54 assert(typeid(typeof(x3)) == typeid(int)); 55 56 Foo!(int*).t4 x4; 57 assert(typeid(typeof(x4)) == typeid(int*)); 58 59 Foo!(int).t5 x5; 60 assert(typeid(typeof(x5)) == typeid(int)); 61 62 Foo!(int).X x6; 63 assert(typeid(typeof(x6)) == typeid(int)); 64 } 65 66 /*********************************************************/ 67 68 test2()69void test2() 70 { 71 alias int T; 72 73 static if (is(T : int)) 74 alias T t1; 75 76 static if (T.sizeof == 4) 77 alias T t2; 78 79 static if (is(T U : int)) 80 alias U t3; 81 82 static if (is(T* V : V*)) 83 alias V t4; 84 85 static if (is(T W)) 86 alias W t5; 87 else 88 alias char t5; 89 90 static if (is(T* X : X*)) 91 { 92 } 93 94 t1 x1; 95 assert(typeid(typeof(x1)) == typeid(int)); 96 97 t2 x2; 98 assert(typeid(typeof(x2)) == typeid(int)); 99 100 t3 x3; 101 assert(typeid(typeof(x3)) == typeid(int)); 102 103 t4 x4; 104 assert(typeid(typeof(x4)) == typeid(int)); 105 106 t5 x5; 107 assert(typeid(typeof(x5)) == typeid(int)); 108 109 X x6; 110 assert(typeid(typeof(x6)) == typeid(int)); 111 } 112 113 /*********************************************************/ 114 test3()115void test3() 116 { 117 static if (is(short : int)) 118 { printf("1\n"); 119 } 120 else 121 assert(0); 122 static if (is(short == int)) 123 assert(0); 124 static if (is(int == int)) 125 { printf("3\n"); 126 } 127 else 128 assert(0); 129 } 130 131 /*********************************************************/ 132 133 template TValue(int i:1) 134 { 135 pragma(msg,"last instantiation!!!"); 136 const int TValue = 1; 137 } 138 TValue(int i)139template TValue(int i) 140 { 141 pragma(msg,"instantiating..."); 142 const int TValue = i * TValue!(i-1); 143 } 144 test4()145void test4() 146 { 147 assert(TValue!(3) == 6); 148 } 149 150 /*********************************************************/ 151 152 template Reverse(string s: "") { 153 const char[] Reverse = ""; 154 } 155 Reverse(string s)156template Reverse(string s) { 157 const char[] Reverse = Reverse!(s[1..$]) ~ s[0]; 158 } 159 test5()160void test5() 161 { 162 assert(Reverse!("Recursive string template") == "etalpmet gnirts evisruceR"); 163 } 164 165 /*********************************************************/ 166 foo6(alias V)167template foo6(alias V) 168 { 169 int foo6() 170 { 171 return V; 172 } 173 } 174 bar6(alias V)175class bar6(alias V) 176 { 177 int abc() 178 { 179 return V; 180 } 181 } 182 test6()183void test6() 184 { 185 int j = 3; 186 int k = 4; 187 188 int i = foo6!(j)(); 189 i += foo6!(j)(); 190 191 i += foo6!(k)(); 192 193 bar6!(j) b = new bar6!(j); 194 i -= b.abc(); 195 196 assert(i == 7); 197 } 198 199 /*********************************************************/ 200 Bind7(alias dg)201template Bind7(alias dg) 202 { 203 int Bind7() 204 { 205 dg('c'); 206 return 0; 207 } 208 } 209 test7()210void test7() 211 { 212 char[] v; 213 214 void foo(char c) { v ~= c; } 215 216 alias Bind7!(foo) intv; 217 intv(); 218 assert(v[0] == 'c'); 219 } 220 221 /*********************************************************/ 222 sum8(real x)223template sum8(real x) 224 { 225 static if (x <= 1.0L){ 226 const real sum8 = x; 227 }else{ 228 const real sum8 = x + sum8!(x - 1.0L); 229 } 230 } 231 test8()232void test8() 233 { 234 real x = sum8!(3.0L); 235 236 if(x != 6.0L){ 237 assert(0); 238 } 239 } 240 241 /*********************************************************/ 242 main()243int main() 244 { 245 test1(); 246 test2(); 247 test3(); 248 test4(); 249 test5(); 250 test6(); 251 test7(); 252 test8(); 253 254 printf("Success\n"); 255 return 0; 256 } 257