1 // REQUIRED_ARGS: 2 3 module template1; 4 5 import core.stdc.stdio : printf; 6 import core.vararg; 7 8 /******************************************/ 9 10 template TFoo1(T,U) 11 { 12 int foo(T t, U u) 13 { 14 return 3; 15 } 16 } 17 18 19 alias TFoo1!(int, char) Foo1; 20 21 22 void test1() 23 { 24 int i = Foo1.foo(1, 2); 25 assert(i == 3); 26 } 27 28 /******************************************/ 29 30 template TFoo2(T,U) 31 { 32 T x = 4; 33 U y; 34 } 35 36 37 alias TFoo2!(int, char) Foo2; 38 39 40 void test2() 41 { 42 assert(Foo2.x + Foo2.y == 0x103); 43 Foo2.x = 3; 44 Foo2.y = 7; 45 assert(Foo2.x + Foo2.y == 10); 46 } 47 48 /******************************************/ 49 50 template TFoo3(T,U) 51 { 52 class Bar 53 { 54 T x = 4; 55 U y; 56 } 57 } 58 59 60 alias TFoo3!(int, char) Foo3; 61 62 63 void test3() 64 { 65 Foo3.Bar b = new Foo3.Bar(); 66 67 assert(b.x == 4); 68 assert(b.y == 0xFF); 69 } 70 71 /******************************************/ 72 73 template TFoo4(T,U) 74 { 75 T x; 76 U y; 77 } 78 79 template TFoo4(T:T,U:T) 80 { 81 T a; 82 U b; 83 } 84 85 template TFoo4(T:uint, U:uint) 86 { 87 T c; 88 U d; 89 } 90 91 alias TFoo4!(int, char) Foo4x; 92 93 void test4() 94 { 95 alias TFoo4!(int, char) Foo4; 96 int* x = &Foo4.c; 97 char* y = &Foo4.d; 98 99 alias TFoo4!(uint, char**) Foo4_2; 100 uint* x2 = &Foo4_2.x; 101 char*** y2 = &Foo4_2.y; 102 103 alias TFoo4!(int, int) Foo4_3; 104 int* x3 = &Foo4_3.a; 105 int* y3 = &Foo4_3.b; 106 107 alias TFoo4!(uint, uint) Foo4_4; 108 uint* x4 = &Foo4_4.c; 109 uint* y4 = &Foo4_4.d; 110 } 111 112 113 /******************************************/ 114 115 template TtoUx(T, U) 116 { 117 T toUx(U[] s) 118 { 119 uint v = 0; 120 121 if (v != cast(T)v) 122 return 3; 123 124 return cast(T)v; 125 } 126 127 } 128 129 alias TtoUx!(ubyte, char).toUx toUbyte; 130 alias TtoUx!(ushort, char).toUx toUshort; 131 132 void test5() 133 { 134 } 135 136 137 /******************************************/ 138 139 template TtoUx6(T, U) 140 { 141 T toUx(U[] s) 142 { 143 uint v = 0; 144 145 if (v != cast(T)v) 146 return 3; 147 148 return cast(T)v; 149 } 150 151 } 152 153 alias TtoUx6!(ubyte, char) t6; 154 155 void test6() 156 { 157 } 158 159 /******************************************/ 160 161 template A7(T) { 162 T id(T t) { 163 return t; 164 } 165 } 166 167 alias A7!(int) a; 168 169 void test7() 170 { 171 printf("%d\r\n", a.id(3)); 172 assert(a.id(3) == 3); 173 } 174 175 /******************************************/ 176 177 template Swapper(T) 178 { 179 void Swap(ref T a, ref T b) 180 { 181 T temp = a; a = b; b = temp; 182 } 183 } 184 185 void test8() 186 { 187 alias Swapper!(int) IntSwap; 188 int a=1,b=2; 189 IntSwap.Swap(a,b); 190 printf("a=%d,b=%d\n",a,b); // prints 2,1 191 assert(a == 2); 192 assert(b == 1); 193 } 194 195 196 /******************************************/ 197 198 template Foo9(T) 199 { 200 class B 201 { 202 T data; 203 } 204 } 205 206 void test9() 207 { 208 (new Foo9!(int).B).data += 4; 209 } 210 211 212 /******************************************/ 213 214 template A10(T) { 215 } 216 217 template B10(T) { 218 alias A10!(int) a; 219 } 220 221 void test10() 222 { 223 alias B10!(int) b; 224 } 225 226 /******************************************/ 227 228 template A11(T) { 229 T idf(T t) { 230 return t; 231 } 232 } 233 234 template B11(T) { 235 private alias A11!(T) a; 236 T same(T t) { 237 return a.idf(t); 238 } 239 } 240 241 void test11() 242 { 243 alias B11!(int) b; 244 //printf("%d\r\n", b.same(10)); 245 assert(b.same(10) == 10); 246 } 247 248 249 /******************************************/ 250 251 template A12(T) { 252 class B { 253 invariant() { 254 assert(1); 255 } 256 T ide(T t) { 257 return t; 258 } 259 } 260 } 261 262 void test12() 263 { 264 alias A12!(int) a; 265 a.B b = new a.B(); 266 printf("%d\r\n", b.ide(10)); 267 assert(b.ide(10) == 10); 268 } 269 270 271 /******************************************/ 272 273 template A13(T) { 274 public interface I { 275 public T i(); 276 } 277 } 278 279 class B13 : A13!(int).I { 280 public int i() { 281 return 42; 282 } 283 } 284 285 void test13() 286 { 287 B13 b = new B13(); 288 A13!(int).I i = b; 289 assert(b.i() == 42); 290 assert(i.i() == 42); 291 } 292 293 294 /******************************************/ 295 296 class B14 297 { 298 } 299 300 template A14(T, U) { 301 302 private U u; 303 304 static this() 305 { 306 u = new U(); 307 } 308 } 309 310 alias A14!(int, B14) t14; 311 312 void test14() 313 { 314 } 315 316 317 /******************************************/ 318 319 template A15(T) { 320 public interface Init { 321 public T init(); 322 } 323 } 324 template A15(T : int) { 325 public class Init { 326 public T init() { 327 return 42; 328 }; 329 } 330 } 331 template A15(T : float) { 332 public class Init { 333 public T init() { 334 return 3.25; 335 }; 336 } 337 } 338 339 template TB15(T, U) { 340 private U initializer; 341 private void setInitializer(U init) { 342 initializer = init; 343 } 344 public class B { 345 private T _value; 346 public this() { 347 this._value = initializer.init(); 348 } 349 public T value() { 350 return this._value; 351 } 352 } 353 } 354 template TB15(T) { 355 private alias TB15!(T, A15!(T).Init) tb; 356 private void setInitializer(A15!(T).Init init) { 357 tb.setInitializer(init); 358 } 359 public class B : tb.B { 360 } 361 } 362 363 void test15() 364 { 365 alias TB15!(int, A15!(int).Init) tb; 366 tb.setInitializer(new A15!(int).Init()); 367 tb.B b = new tb.B(); 368 int i; 369 i = b.value(); 370 assert(i == 42); 371 372 alias TB15!(float) tb2; 373 tb2.setInitializer(new A15!(float).Init()); 374 tb2.B b2 = new tb2.B(); 375 assert(b2.value() == 3.25); 376 } 377 378 /******************************************/ 379 380 template foo16(U : int, int T : 9+1) 381 { 382 U x = T; 383 } 384 385 alias foo16!(int, 10) bar16; 386 387 void test16() 388 { 389 int i; 390 391 i = bar16.x; 392 assert(i == 10); 393 assert(foo16!(int, 10).x == 10); 394 } 395 396 /******************************************/ 397 398 template VecTemplate(tfloat) 399 { 400 struct Vector 401 { 402 tfloat d; 403 } 404 } 405 406 void test17() 407 { 408 with (VecTemplate!(int)) // crash DMD 409 { 410 } 411 } 412 413 /******************************************/ 414 415 template Bomb (T) 416 { 417 void foo (T *parm) 418 { 419 } 420 } 421 422 template Name (T) 423 { 424 T y; 425 426 void test () 427 { 428 Bomb!(T).foo (&y); 429 } 430 } 431 432 alias Name!(int) a18; 433 alias Name!(ubyte) b18; 434 435 void test18() 436 { 437 } 438 439 /******************************************/ 440 441 template one20( T ) 442 { 443 alias T function () safeptr; 444 } 445 446 template one20( T1, T2 ) 447 { 448 alias int function(int) safeptr; 449 } 450 451 alias one20!( int ) A; 452 A.safeptr foo20; 453 454 alias one20!( int, int ) B; 455 B.safeptr bar20; 456 457 458 int func_bar(int i) { return 2; } 459 460 void test20() 461 { 462 bar20 = &func_bar; 463 } 464 465 /******************************************/ 466 467 class A21 { int x; } 468 class B21 : A21 { int y; } 469 470 void abc21(B21* b) { } 471 472 template TFoo21(T : A21, U : T*) 473 { 474 void test() 475 { 476 assert(T.sizeof == B21.sizeof); 477 U u; 478 abc21(u); 479 } 480 } 481 482 alias TFoo21!(B21, B21*) bar21; 483 484 void test21() 485 { 486 bar21.test(); 487 } 488 489 /******************************************/ 490 491 template Bug22(T : Object) { 492 int print() { 493 printf("Bug22(T : Object).print()\r\n"); 494 return 1; 495 } 496 } 497 template Bug22(T) { 498 int print() { 499 printf("Bug22(T).print()\r\n"); 500 return 2; 501 } 502 } 503 template TTest22(T) { 504 private alias Bug22!(T) bug; 505 class Test { 506 int test() { 507 return bug.print(); 508 } 509 } 510 } 511 512 void test22() 513 { 514 alias TTest22!(int).Test Test1; 515 alias TTest22!(Test1).Test Test2; 516 alias TTest22!(Object).Test Test3; 517 Test1 test1 = new Test1(); 518 Test2 test2 = new Test2(); 519 Test3 test3 = new Test3(); 520 int i; 521 522 i = test1.test(); 523 assert(i == 2); 524 i = test2.test(); 525 assert(i == 1); 526 i = test3.test(); 527 assert(i == 1); 528 } 529 530 531 /******************************************/ 532 533 template T23() 534 { 535 struct Rank 536 { 537 } 538 } 539 540 template A23() 541 { 542 struct Array 543 { 544 alias T23!().Rank Rank1; 545 546 Rank1 data; 547 } 548 } 549 550 alias A23!().Array Array_int23; 551 552 void test23() 553 { 554 } 555 556 557 /******************************************/ 558 559 template TList24(T) 560 { 561 class Node 562 { 563 } 564 class List 565 { 566 Node m_first = null; 567 } 568 } 569 570 void test24() 571 { 572 alias TList24!(uint).List UIntList; 573 } 574 575 576 /******************************************/ 577 578 template TList25(T) 579 { 580 class Node 581 { 582 Node prev; 583 Node next; 584 T Value; 585 } 586 class List 587 { 588 Node m_first = null; 589 Node m_last = null; 590 void AddFront(T _Value) 591 { 592 Node cur = new Node; 593 with (cur) 594 { 595 next = m_first; 596 prev = null; 597 Value = _Value; 598 if (next !is null) 599 next.prev = cur; 600 } 601 m_first = null; 602 if (m_last is null) 603 m_last = cur; 604 } 605 } 606 } 607 608 void test25() 609 { 610 alias TList25!(uint).List UIntList; 611 alias TList25!(uint).Node UIntNode; 612 UIntList list; 613 UIntNode node; 614 for (int i = 1; i <= 10; i++) 615 {} //list.AddFront(i); 616 } 617 618 619 /******************************************/ 620 621 template Foo26(T) 622 { 623 void doIt() { 624 printf("Foo26(T)\r\n"); 625 } 626 } 627 628 template Foo26(T : T[]) 629 { 630 private alias Foo26!(T) bug; 631 void doIt() { 632 printf("Foo26(T[])\r\n"); 633 bug.doIt(); 634 } 635 } 636 637 void test26() 638 { 639 alias Foo26!(int[]) foo; 640 foo.doIt(); 641 } 642 643 644 /******************************************/ 645 646 template Foo27(T) 647 { 648 public const T[] empty = []; 649 } 650 651 void test27() 652 { 653 alias Foo27!(int) bug; 654 } 655 656 657 /******************************************/ 658 659 template A28(T) { 660 public bool all(in T[] array, bool function (T) predicate) { 661 for (int i = 0; i < array.length; i++) { 662 if (!predicate(array[i])) { 663 return false; 664 } 665 } 666 return true; 667 } 668 } 669 670 void test28() 671 { 672 static bool isVowel(char c) { 673 return (c == 'a') || (c == 'e') || (c == 'i') || (c == 'o') || (c == 'u'); 674 } 675 676 alias A28!(char) arrays; 677 assert(arrays.all("aeiouoeieuiei", &isVowel)); 678 assert(arrays.all("aeoiuaioeuioaeuiouoiaeu", &isVowel)); 679 assert(!arrays.all("aaeiouioeujiurioue", &isVowel)); 680 assert(!arrays.all("bjkqwkjbwqjbkwb", &isVowel)); 681 assert(arrays.all("", &isVowel)); 682 printf("A28(char).all tests passed!\r\n"); 683 } 684 685 686 /******************************************/ 687 688 public template TRange29(T) { 689 debug private bool recursing = false; 690 public class Range { 691 private T _lower; 692 private T _upper; 693 public this(T lower, T upper) { 694 this._lower = lower; 695 this._upper = upper; 696 } 697 public T lower() { 698 return this._lower; 699 } 700 public T upper() { 701 return this._upper; 702 } 703 public bool contains(T item) { 704 return (lower() <= item) && (item <= upper()); 705 } 706 public bool intersects(Range other) 707 in { 708 assert(other !is null); 709 } out (result) { 710 debug { 711 if (!recursing) { 712 recursing = true; 713 assert(result == other.intersects(this)); 714 } else { 715 recursing = false; 716 } 717 } 718 } do { 719 return contains(other.lower()) || contains(other.upper()) || other.includes(this); 720 } 721 public bool includes(Range other) 722 in { 723 assert(other !is null); 724 } out (result) { 725 assert(result == (contains(other.lower()) && contains(other.upper()))); 726 } do { 727 return contains(other.lower()) && contains(other.upper()); 728 } 729 } 730 } 731 732 void test29() 733 { 734 alias TRange29!(int).Range Range; 735 Range r1 = new Range(1, 10); 736 Range r2 = new Range(5, 15); 737 assert(r1.intersects(r2) == 1); 738 } 739 740 741 /******************************************/ 742 743 template TCopy30(T) 744 { 745 void copy(out T to, T from) 746 { 747 to = from; 748 } 749 } 750 751 template TCopy30(T : string) 752 { 753 void copy(out string to, in string from) 754 { 755 printf("Specialization\n"); 756 to = from; 757 } 758 } 759 760 void test30() 761 { 762 int i = 0; 763 float f = 0; 764 string s; 765 766 alias TCopy30!(int) copyint; 767 alias TCopy30!(string) copystr; 768 769 copyint.copy(i, 3); 770 printf("%d\n", i); 771 assert(i == 3); 772 773 copystr.copy(s, "Here it comes"); 774 printf("%.*s\n", cast(int)s.length, s.ptr); 775 assert(s == "Here it comes"); 776 } 777 778 /******************************************/ 779 780 import core.demangle; 781 782 template Foo31(alias X) 783 { 784 alias X.demangle y; 785 } 786 787 void test31() 788 { 789 alias Foo31!(core.demangle) bar; 790 } 791 792 793 /******************************************/ 794 795 shared int x32; 796 797 template Foo32(alias X) 798 { 799 static shared int* p = &X; 800 } 801 802 alias Foo32!(x32) abc32; 803 804 void test32() 805 { 806 alias Foo32!(x32) bar; 807 808 *bar.p = 3; 809 assert(x32 == 3); 810 811 *abc32.p = 4; 812 assert(x32 == 4); 813 } 814 815 /******************************************/ 816 817 shared int x33; 818 819 template Foo33(alias X) 820 { 821 static shared int* p = &X; 822 } 823 824 template Bar33(alias T) 825 { 826 alias T!(x33) abc; 827 } 828 829 void test33() 830 { 831 alias Bar33!(Foo33) bar; 832 833 *bar.abc.p = 3; 834 assert(x33 == 3); 835 } 836 837 /******************************************/ 838 839 shared int x34; 840 841 template Foo34(alias X) 842 { 843 static shared int* p = &X; 844 } 845 846 template Bar34(alias T) 847 { 848 alias T.p q; 849 } 850 851 void test34() 852 { 853 alias Foo34!(x34) foo; 854 alias Bar34!(foo) bar; 855 856 *bar.q = 3; 857 assert(x34 == 3); 858 } 859 860 /******************************************/ 861 862 class Foo35 863 { 864 static int p; 865 } 866 867 template Bar35(alias T) 868 { 869 alias T.p q; 870 } 871 872 void test35() 873 { 874 alias Bar35!(Foo35) bar; 875 876 bar.q = 3; 877 assert(Foo35.p == 3); 878 } 879 880 /******************************************/ 881 882 template Bar36(T) 883 { 884 class Bar36 885 { 886 static T x; 887 }; 888 } 889 890 void test36() 891 { 892 Bar36!(int).x = 3; 893 } 894 895 /******************************************/ 896 897 class Bar37(T) 898 { 899 static T x; 900 } 901 902 903 void test37() 904 { 905 Bar37!(int).x = 3; 906 } 907 908 /******************************************/ 909 910 class Bar38(T) 911 { 912 static T x = 3; 913 } 914 915 916 void test38() 917 { 918 int i = template1.Bar38!(int).x; 919 assert(i == 3); 920 921 int j = Bar38!(int).x; 922 assert(j == 3); 923 } 924 925 /******************************************/ 926 927 class Bar39(T) 928 { 929 alias T x; 930 } 931 932 933 void test39() 934 { 935 Bar39!(int).x y = 3; 936 assert(y == 3); 937 } 938 939 940 /******************************************/ 941 942 template Bar40(T) 943 { 944 alias T Bar40; 945 } 946 947 948 void test40() 949 { 950 Bar40!(int) y = 3; 951 assert(y == 3); 952 } 953 954 /******************************************/ 955 956 template Bar41(T) 957 { 958 alias T Bar41; 959 } 960 961 962 void test41() 963 { 964 template1.Bar41!(int) y = 3; 965 assert(y == 3); 966 967 assert(template1.Bar41!(int).sizeof == int.sizeof); 968 } 969 970 /******************************************/ 971 972 template Bar42(T) { T t; } 973 974 typeof(Bar42!(int).t) bar42; 975 976 void test42() 977 { 978 bar42 = 5; 979 } 980 981 /******************************************/ 982 983 template factor43(int n : 1) 984 { 985 enum { value = 1 } 986 } 987 988 template factor43(int n) 989 { 990 enum { value = n*factor43!(n-1).value } 991 } 992 993 void test43() 994 { 995 996 int i = factor43!(3).value; 997 998 printf("%d\n",i); 999 assert(i == 6); 1000 } 1001 1002 1003 /******************************************/ 1004 1005 template factorial1(int n : 1) 1006 { 1007 const int x = 1; 1008 } 1009 1010 template factorial1(int n) 1011 { 1012 const int x = n*.factorial1!(n-1).x; 1013 } 1014 1015 template factorial2(int n : 1) 1016 { 1017 const int factorial2 = 1; 1018 } 1019 1020 template factorial2(int n) 1021 { 1022 const int factorial2 = n*.factorial2!(n-1); 1023 } 1024 1025 template factorial3(int n : 1) 1026 { 1027 enum { x = 1 } 1028 } 1029 1030 template factorial3(int n) 1031 { 1032 enum { x = n*.factorial3!(n-1).x } 1033 } 1034 1035 template factorial4(int n : 1) 1036 { 1037 enum { factorial4 = 1 } 1038 } 1039 1040 template factorial4(int n) 1041 { 1042 enum { factorial4 = n*.factorial4!(n-1) } 1043 } 1044 1045 void test44() 1046 { 1047 1048 int i = factorial1!(4).x; 1049 printf("%d\n",i); 1050 assert(i == 24); 1051 1052 i = factorial2!(4); 1053 printf("%d\n",i); 1054 assert(i == 24); 1055 1056 i = factorial3!(4).x; 1057 printf("%d\n",i); 1058 assert(i == 24); 1059 1060 i = factorial4!(4); 1061 printf("%d\n",i); 1062 assert(i == 24); 1063 } 1064 1065 1066 /******************************************/ 1067 1068 template factor45(int n) 1069 { 1070 int value() 1071 { 1072 if (n==0 || n==1) 1073 return 1; 1074 return n * factor45!(n-1).value(); 1075 } 1076 } 1077 1078 template factor45(int n : 0) 1079 { 1080 int value() 1081 { 1082 return 1; 1083 } 1084 } 1085 1086 template factor45(int n : 1) 1087 { 1088 int value() 1089 { 1090 return 1; 1091 } 1092 } 1093 1094 void test45() 1095 { 1096 int i; 1097 1098 i = factor45!(4).value(); 1099 printf( "%d\n", i); 1100 assert(i == 24); 1101 } 1102 1103 1104 /******************************************/ 1105 1106 template sqrt46(int n, int lo, int hi : lo) 1107 { 1108 enum { result = lo } 1109 } 1110 1111 void test46() 1112 { 1113 int i; 1114 1115 i = sqrt46!(1, 24, 24).result; 1116 printf("i = %d\n", i); 1117 assert(i == 24); 1118 } 1119 1120 /******************************************/ 1121 1122 template sqrt47(int n, int lo, int hi) 1123 { 1124 enum { mid = (lo + hi + 1) / 2 } 1125 1126 enum { result = (n < mid * mid) ? sqrt47!(n, lo, mid - 1).result 1127 : sqrt47!(n, mid, hi).result } 1128 } 1129 1130 template sqrt47(int n, int lo, int hi : lo) 1131 { 1132 enum { result = lo } 1133 } 1134 1135 template sqrt47(int n) 1136 { 1137 enum { sqrt47 = .sqrt47!(n, 1, n).result } 1138 } 1139 1140 void test47() 1141 { 1142 int i; 1143 1144 i = sqrt47!(24); 1145 printf("i = %d\n", i); 1146 } 1147 1148 /******************************************/ 1149 1150 class Foo48 (T) 1151 { 1152 alias T Type; 1153 1154 class Inner (U) 1155 { 1156 alias U Type; 1157 }; 1158 }; 1159 1160 struct Bar48 (alias TT) 1161 { 1162 alias TT!(int).Type A; 1163 alias TT!(int).Inner!(A).Type B; 1164 }; 1165 1166 void test48() 1167 { 1168 Bar48!(Foo48).A x; 1169 Bar48!(Foo48).B y; 1170 1171 int *p; 1172 1173 p = &x; 1174 p = &y; 1175 } 1176 1177 1178 /******************************************/ 1179 1180 struct Foo49(T) 1181 { 1182 static Foo49 bar(T c1) 1183 { 1184 Foo49 rtn; // Error here 1185 return rtn; 1186 } 1187 } 1188 1189 void test49() 1190 { 1191 alias Foo49!(double) vector; 1192 1193 vector.bar(1); 1194 } 1195 1196 /******************************************/ 1197 1198 struct Foo50(T) 1199 { 1200 T x = 0; 1201 1202 static Foo50 bar(T c1) 1203 { 1204 .Foo50!(typeof(c1)) rtn; 1205 rtn.x = c1; 1206 return rtn; 1207 } 1208 1209 static .Foo50!(T) barx(T c1) 1210 { 1211 Foo50 rtn; 1212 rtn.x = c1; 1213 return rtn; 1214 } 1215 } 1216 1217 void test50() 1218 { 1219 alias Foo50!(double) vector; 1220 1221 vector xAxis = vector.bar(1); 1222 } 1223 1224 /******************************************/ 1225 1226 struct Foo51(T) 1227 { 1228 T x = 0; 1229 .Foo51!(long)* p; 1230 1231 static Foo51 bar(T c1) 1232 { 1233 .Foo51!(typeof(c1)) rtn; 1234 rtn.x = c1; 1235 return rtn; 1236 } 1237 1238 static .Foo51!(T) barx(T c1) 1239 { 1240 Foo51 rtn; 1241 .Foo51!(int)* c; 1242 rtn.x = c1; 1243 return rtn; 1244 } 1245 } 1246 1247 void test51() 1248 { 1249 alias Foo51!(double) vector; 1250 1251 vector xAxis = vector.bar(1); 1252 } 1253 1254 1255 /******************************************/ 1256 1257 interface Interface(T) 1258 { 1259 void foo52(); 1260 } 1261 1262 void bar52(Interface!(Object) i) 1263 { 1264 i.foo52(); 1265 } 1266 1267 class Abstract(T) : Interface!(T) 1268 { 1269 abstract void foo52(); 1270 } 1271 1272 class Concrete(T) : Abstract!(T) 1273 { 1274 override void foo52() { printf("Concrete.foo52(this = %p)\n", this); } 1275 } 1276 1277 class Sub(T) : Concrete!(T) 1278 { 1279 } 1280 1281 void test52() 1282 { 1283 Sub!(Object) s = new Sub!(Object)(); 1284 s.foo52(); 1285 bar52(s); 1286 } 1287 1288 1289 /******************************************/ 1290 1291 class Foo53 1292 { 1293 template tmethod (T) 1294 { 1295 public static void tmethod (T param) 1296 { 1297 printf("param = %d\n", param); 1298 assert(param == 42); 1299 } 1300 } 1301 } 1302 1303 1304 void test53() 1305 { 1306 Foo53 foo = new Foo53; 1307 1308 Foo53.tmethod!(int)(42); 1309 } 1310 1311 1312 /******************************************/ 1313 1314 class Foo54 1315 { 1316 template func(W) { 1317 static void foo(W w) { printf("W_I %d\n", w); assert(w == 3); } 1318 static int xx; 1319 } 1320 } 1321 1322 void test54() { 1323 1324 Foo54 c = new Foo54(); 1325 c.func!(int).foo(3); 1326 c.func!(int).xx = 4; 1327 1328 } 1329 1330 /******************************************/ 1331 1332 template T55(S) 1333 { 1334 struct Foo55 1335 { 1336 static Foo55 test(Foo55 f) 1337 { 1338 Foo55 a = f; 1339 return f; 1340 } 1341 } 1342 } 1343 1344 alias T55!(char).Foo55 Foo55; 1345 alias T55!(char).Foo55 Bar55; 1346 1347 1348 void test55() 1349 { 1350 Bar55 a; 1351 Foo55 b; 1352 b.test(a); 1353 Bar55.test(a); 1354 } 1355 1356 /******************************************/ 1357 1358 template CT56(T) 1359 { 1360 class C 1361 { 1362 const char[][1] arrArr=["foo" ]; 1363 } 1364 } 1365 1366 void test56() 1367 { 1368 alias CT56!(int) Ct; 1369 Ct.C c= new Ct.C(); 1370 printf("%.*s\n", cast(int)c.arrArr[0].length, c.arrArr[0].ptr); 1371 assert(c.arrArr[0] == "foo"); 1372 } 1373 1374 1375 /******************************************/ 1376 1377 template foo57(T : int = int) 1378 { 1379 T x = 3; 1380 } 1381 1382 void test57() 1383 { 1384 printf("%d\n", foo57!().x); 1385 assert(foo57!().x == 3); 1386 } 1387 1388 /******************************************/ 1389 1390 template Foo58(T, U = T) 1391 { 1392 U x = 3; 1393 } 1394 1395 void test58() 1396 { 1397 alias Foo58!(int) f; 1398 assert(f.x == 3); 1399 assert(f.x.sizeof == 4); 1400 } 1401 1402 /******************************************/ 1403 1404 template Foo59(T, U = T*) 1405 { 1406 shared T x = 3; 1407 shared U px = &x; 1408 } 1409 1410 void test59() 1411 { 1412 alias Foo59!(uint) f; 1413 assert(f.x == 3); 1414 assert(f.x.sizeof == 4); 1415 assert(*f.px == 3); 1416 1417 alias Foo59!(long) g; 1418 assert(g.x == 3); 1419 assert(g.x.sizeof == 8); 1420 assert(*g.px == 3); 1421 } 1422 1423 /******************************************/ 1424 1425 class A60 1426 {} 1427 1428 template B60(T, U = short) 1429 { 1430 struct Thing 1431 { 1432 T t; 1433 U u; 1434 }; 1435 } 1436 1437 template C60(T, U = A60) 1438 { 1439 class C60 1440 : U 1441 {} 1442 1443 class C2 1444 {}; 1445 } 1446 1447 void test60() 1448 { 1449 B60!(int, long).Thing thing1; 1450 B60!(int).Thing thing2; 1451 1452 printf("thing1.sizeof: %zu\n", thing1.sizeof); 1453 printf("thing2.sizeof: %zu\n", thing2.sizeof); 1454 1455 assert(thing1.sizeof == long.alignof + long.sizeof); 1456 assert(thing2.sizeof == 8); 1457 1458 C60!(int /*,A60*/ ) container1; 1459 1460 printf("container1.sizeof: %zu\n", container1.sizeof); 1461 assert(container1.sizeof == (void*).sizeof); 1462 } 1463 1464 /******************************************/ 1465 1466 struct Foo61 1467 { 1468 int a; 1469 1470 template Bar(T) 1471 { 1472 T abc() { return a; } 1473 } 1474 1475 int def() { return 4; } 1476 } 1477 1478 void test61() 1479 { 1480 Foo61 *f = new Foo61(); 1481 int i; 1482 1483 f.a = 3; 1484 i = f.def(); 1485 assert(i == 4); 1486 i = f.Bar!(int).abc(); 1487 assert(i == 3); 1488 1489 Foo61 g; 1490 g.a = 3; 1491 i = g.def(); 1492 assert(i == 4); 1493 i = g.Bar!(int).abc(); 1494 assert(i == 3); 1495 } 1496 1497 /******************************************/ 1498 1499 class Foo62(T) 1500 { 1501 template Bar(T) 1502 { 1503 int func() { return 3; } 1504 } 1505 } 1506 1507 void test62() 1508 { 1509 Foo62!(int) x = new Foo62!(int); 1510 1511 assert(x.Bar!(int).func() == 3); 1512 } 1513 1514 /******************************************/ 1515 1516 class Foo63(T) 1517 { 1518 template Bar(T) 1519 { 1520 int func() { this.def(); return 3; } 1521 int func2() { return 4; } 1522 } 1523 1524 void def() 1525 { 1526 assert(Bar!(T).func2() == 4); 1527 } 1528 } 1529 1530 void test63() 1531 { 1532 Foo63!(int) x = new Foo63!(int); 1533 1534 assert(x.Bar!(int).func() == 3); 1535 x.def(); 1536 } 1537 1538 /******************************************/ 1539 1540 struct XVector(qfloat) 1541 { 1542 qfloat x;qfloat y;qfloat z; 1543 1544 static int opCall (qfloat x, qfloat y, qfloat z) { return 8; } 1545 } 1546 1547 void test64() 1548 { 1549 int i; 1550 i = XVector!(int)(1,2,3); 1551 assert(i == 8); 1552 i = XVector!(real).opCall(1,2,3); 1553 assert(i == 8); 1554 } 1555 1556 /******************************************/ 1557 // http://www.digitalmars.com/d/archives/28052.html 1558 1559 alias int value_type; 1560 1561 struct Foo65 1562 { 1563 uint length() { return 47; } 1564 1565 size_t test() 1566 { 1567 value_type[] e = new value_type[length]; 1568 return e.length; 1569 } 1570 } 1571 1572 void test65() 1573 { 1574 Foo65 f; 1575 1576 assert(f.test() == 47); 1577 } 1578 1579 /******************************************/ 1580 1581 class Thing66 1582 { 1583 template print(T2) 1584 { 1585 void print(T2 t) 1586 { 1587 printf("t = %d\n", t); 1588 assert(t == 10); 1589 } 1590 } 1591 } 1592 1593 1594 void test66() 1595 { 1596 Thing66 thing = new Thing66; 1597 1598 thing.print!(int)(10); 1599 } 1600 1601 /******************************************/ 1602 1603 template Foo67(alias T) 1604 { 1605 void Foo67() 1606 { 1607 printf("T = '%.*s'\n", cast(int)T.length, T.ptr); 1608 assert(T == "hello"); 1609 } 1610 } 1611 1612 void test67() 1613 { 1614 static string x = "hello"; 1615 1616 Foo67!(x)(); 1617 } 1618 1619 1620 /******************************************/ 1621 1622 template T68(int a) { 1623 int[a] vec; 1624 } 1625 1626 void test68() 1627 { 1628 int i; 1629 1630 i = T68!(4>1?4:1).vec[0]; 1631 assert(i == 0); 1632 i = T68!(4==1?1:(1==1?4:(4>1?1:4))).vec[0]; 1633 assert(i == 0); 1634 } 1635 1636 /******************************************/ 1637 1638 size_t printx(string s) 1639 { 1640 printf("s = '%.*s'\n", cast(int)s.length, s.ptr); 1641 return s.length; 1642 } 1643 1644 size_t printx(int i) 1645 { 1646 printf("i = %d\n", i); 1647 return 28; 1648 } 1649 1650 template Foo69(alias T) 1651 { 1652 size_t Foo69() 1653 { 1654 return printx(T); 1655 } 1656 } 1657 1658 void test69() 1659 { 1660 static string x = "hello"; 1661 static string z = "abc"; 1662 static int y=100; 1663 size_t i; 1664 1665 alias Foo69!(x) foox; 1666 alias Foo69!(y) fooy; 1667 1668 i = Foo69!(x)(); 1669 assert(i == 5); 1670 i = Foo69!(y)(); 1671 assert(i == 28); 1672 i = Foo69!(z)(); 1673 assert(i == 3); 1674 i = foox(); 1675 assert(i == 5); 1676 i = fooy(); 1677 assert(i == 28); 1678 } 1679 1680 /******************************************/ 1681 1682 template temptt70(alias func) 1683 { 1684 void temp() 1685 { 1686 func(); 1687 } 1688 } 1689 1690 int x70; 1691 1692 void myfunc70() 1693 { 1694 printf("myfunc70()\n"); 1695 x70 = 6; 1696 } 1697 1698 alias temptt70!(myfunc70).temp foo70; 1699 1700 void test70() 1701 { 1702 foo70(); 1703 assert(x70 == 6); 1704 } 1705 1706 /******************************************/ 1707 1708 struct A71(T) 1709 { 1710 alias .A71!(T) AT; 1711 int x; 1712 } 1713 1714 alias A71!(int) Aint71; 1715 1716 void test71() 1717 { 1718 Aint71.AT a; 1719 a.x = 3; 1720 } 1721 1722 /******************************************/ 1723 1724 template foo72(T) 1725 { 1726 char[] foo72(T d) 1727 { 1728 uint sz = typeof(d[0]).sizeof * 2; 1729 return null; 1730 } 1731 } 1732 1733 void test72() 1734 { 1735 static ulong[5] a = [0,1,2,3,4]; 1736 static uint[5] b = [0,1,2,3,4]; 1737 char[] r; 1738 r = foo72!(ulong[5])(a); printf("%.*s\n", cast(int)r.length, r.ptr); 1739 r = foo72!(uint[5])(b); printf("%.*s\n", cast(int)r.length, r.ptr); 1740 } 1741 1742 1743 /******************************************/ 1744 1745 alias int Int73; 1746 class Test73(T = Int73); 1747 alias Test73!() Foo73; 1748 1749 void test73() 1750 { 1751 } 1752 1753 /******************************************/ 1754 1755 class A74 1756 { 1757 alias A74 atype; 1758 int x; 1759 } 1760 1761 1762 class B74(R, int V = R.sizeof) 1763 { 1764 int v = V; 1765 } 1766 1767 void test74() 1768 { 1769 B74!(A74,3) b = new B74!(A74,3)(); 1770 assert(b.v == 3); 1771 1772 B74!(A74) c = new B74!(A74)(); 1773 assert(c.v == A74.sizeof); 1774 } 1775 1776 1777 /******************************************/ 1778 1779 interface NotionalRange75(V) 1780 { 1781 } 1782 1783 class MatchedNotionalRange75(R) 1784 : NotionalRange75!(R.value_type) 1785 { 1786 } 1787 1788 class Range75 1789 { 1790 alias int value_type; 1791 } 1792 1793 class List75 1794 { 1795 1796 MatchedNotionalRange75!(Range75) x; 1797 } 1798 1799 void test75() 1800 { 1801 } 1802 1803 1804 /******************************************/ 1805 1806 interface Indian(T) 1807 { 1808 } 1809 1810 interface Iterable(T) 1811 { 1812 Indian!(T) foo(); 1813 } 1814 1815 class Lope(T) : Iterable!(T) 1816 { 1817 Indian!(T) foo() 1818 { 1819 return new Corn!(T); 1820 } 1821 } 1822 1823 class Corn(T) : Indian!(T) 1824 { 1825 } 1826 1827 void test76() 1828 { 1829 Lope!(int) x = new Lope!(int); 1830 } 1831 1832 1833 /******************************************/ 1834 1835 class RawFile 1836 { 1837 } 1838 1839 class Stream : RawFile 1840 { 1841 template readLineT(T) { bool readLineT() 1842 { 1843 if (super) 1844 return false; 1845 return true; 1846 }} 1847 1848 bool readLine() 1849 { 1850 return readLineT!(int)(); 1851 } 1852 } 1853 1854 void test77() 1855 { 1856 } 1857 1858 1859 /******************************************/ 1860 1861 class Four(U, V, X, Y) 1862 { 1863 U i; V j; X k; Y l; 1864 } 1865 1866 template WhatFour(U,V,X,Y) 1867 { 1868 int func(Four!(U,V,X,Y) four) 1869 { 1870 printf("general template\n"); 1871 return 1; 1872 } 1873 } 1874 1875 template WhatFour(U:int,V,X,Y) 1876 { 1877 int func(Four!(int,V,X,Y) four) 1878 { 1879 printf("specialization:: first int\n"); 1880 return 2; 1881 } 1882 } 1883 1884 template WhatFour(U,V:U,X,Y:X) 1885 { 1886 int func(Four!(U,U,X,X) four) 1887 { 1888 printf("specialization:: first two equal, second two equal\n"); 1889 return 3; 1890 } 1891 } 1892 1893 alias WhatFour!(int,float,char,bool).func whatfour; 1894 alias WhatFour!(float,float,char,bool).func whatfour; 1895 alias WhatFour!(float,float,char,char).func whatfour; 1896 alias WhatFour!(int,int,float,char).func whatfour; // ambiguous match 1897 1898 void test78() 1899 { int j; 1900 1901 Four!(int,float,char,bool) f; 1902 Four!(float,float,char,bool) g; 1903 Four!(float,float,char,char) h; 1904 Four!(int,int,float,char) i; 1905 1906 j = whatfour(f); 1907 assert(j == 2); 1908 j = whatfour(g); 1909 assert(j == 1); 1910 j = whatfour(h); 1911 assert(j == 3); 1912 j = whatfour(i); 1913 assert(j == 2); 1914 1915 /* 1916 will print: 1917 specialization:: first int 1918 general template 1919 specialization:: first two equal, second two equal 1920 specialization:: first int 1921 */ 1922 } 1923 1924 1925 /******************************************/ 1926 // http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D.bugs&artnum=2117 1927 1928 class Conversion(T,U){ 1929 alias char Small; 1930 class Big{ 1931 char[2] dummy; 1932 } 1933 static Small Test(U u); 1934 static Big Test(...); 1935 static T MakeT(); 1936 enum { 1937 exists = (Test(MakeT())).sizeof == (Small).sizeof 1938 } 1939 } 1940 1941 void variadicDummy(...){ 1942 } 1943 1944 void test79() 1945 { 1946 variadicDummy(Conversion!(double,int).exists); 1947 } 1948 1949 /******************************************/ 1950 1951 class A80(T) 1952 { 1953 T s; 1954 1955 int foo(int delegate (T) d) { return 3 + d(s); } 1956 1957 int bar() 1958 { 1959 return foo(delegate int (T t) { return 6 + t.x; }); 1960 } 1961 } 1962 1963 class B80: A80!(B80) 1964 { 1965 int x = 20; 1966 } 1967 1968 class C80: A80!(C80) 1969 { 1970 int y = 3; 1971 int x = 10; 1972 } 1973 1974 void test80() 1975 { 1976 B80 b = new B80(); 1977 C80 c = new C80(); 1978 1979 b.s = b; 1980 c.s = c; 1981 1982 assert(b.bar() == 9+20); 1983 assert(c.bar() == 9+10); 1984 } 1985 1986 /******************************************/ 1987 1988 struct T81(FOO) 1989 { 1990 S81 s; 1991 } 1992 1993 struct S81 1994 { 1995 T81!(int)* pt; 1996 } 1997 1998 void test81() 1999 { 2000 } 2001 2002 /******************************************/ 2003 2004 T foo82(T : const(U)*, U=char)(T t) 2005 { 2006 return null; 2007 } 2008 2009 void test82() 2010 { int i; 2011 const int ci; 2012 2013 //writeln(typeid(typeof(foo82(&ci)))); 2014 //writeln(typeid(typeof(foo82(&i)))); 2015 assert(typeof(foo82(&ci)).stringof == "const(int)*"); 2016 assert(typeof(foo82(&i)).stringof == "int*"); 2017 } 2018 2019 /******************************************/ 2020 2021 struct A83 2022 { 2023 void foo(int) {} 2024 void bar(T)(T) {} 2025 } 2026 2027 void test83() 2028 { 2029 A83 a; 2030 a.foo = 5; 2031 a.bar = 6; 2032 } 2033 2034 /******************************************/ 2035 2036 int main() 2037 { 2038 test1(); 2039 test2(); 2040 test3(); 2041 test4(); 2042 test5(); 2043 test6(); 2044 test7(); 2045 test8(); 2046 test9(); 2047 test10(); 2048 test11(); 2049 test12(); 2050 test13(); 2051 test14(); 2052 test15(); 2053 test16(); 2054 test17(); 2055 test18(); 2056 // test19(); 2057 test20(); 2058 test21(); 2059 test22(); 2060 test23(); 2061 test24(); 2062 test25(); 2063 test26(); 2064 test27(); 2065 test28(); 2066 test29(); 2067 test30(); 2068 test31(); 2069 test32(); 2070 test33(); 2071 test34(); 2072 test35(); 2073 test36(); 2074 test37(); 2075 test38(); 2076 test39(); 2077 test40(); 2078 test41(); 2079 test42(); 2080 test43(); 2081 test44(); 2082 test45(); 2083 test46(); 2084 test47(); 2085 test48(); 2086 test49(); 2087 test50(); 2088 test51(); 2089 test52(); 2090 test53(); 2091 test54(); 2092 test55(); 2093 test56(); 2094 test57(); 2095 test58(); 2096 test59(); 2097 test60(); 2098 test61(); 2099 test62(); 2100 test63(); 2101 test64(); 2102 test65(); 2103 test66(); 2104 test67(); 2105 test68(); 2106 test69(); 2107 test70(); 2108 test71(); 2109 test72(); 2110 test73(); 2111 test74(); 2112 test75(); 2113 test76(); 2114 test77(); 2115 test78(); 2116 test79(); 2117 test80(); 2118 test81(); 2119 test82(); 2120 test83(); 2121 2122 printf("Success\n"); 2123 return 0; 2124 }