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 } body { 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 } body { 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", s.length, s.ptr); 775 assert(s == "Here it comes"); 776 } 777 778 /******************************************/ 779 780 import std..string; 781 782 template Foo31(alias X) 783 { 784 alias X.toStringz y; 785 } 786 787 void test31() 788 { 789 alias Foo31!(std..string) 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", 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: %u\n", thing1.sizeof); 1453 printf("thing2.sizeof: %u\n", thing2.sizeof); 1454 1455 version (Win32) 1456 assert(thing1.sizeof == 16); 1457 else version (X86_64) 1458 assert(thing1.sizeof == 16); 1459 else version(ARM) 1460 assert(thing1.sizeof == 16); 1461 else 1462 assert(thing1.sizeof == 12); 1463 assert(thing2.sizeof == 8); 1464 1465 C60!(int /*,A60*/ ) container1; 1466 1467 printf("container1.sizeof: %u\n", container1.sizeof); 1468 assert(container1.sizeof == (void*).sizeof); 1469 } 1470 1471 /******************************************/ 1472 1473 struct Foo61 1474 { 1475 int a; 1476 1477 template Bar(T) 1478 { 1479 T abc() { return a; } 1480 } 1481 1482 int def() { return 4; } 1483 } 1484 1485 void test61() 1486 { 1487 Foo61 *f = new Foo61(); 1488 int i; 1489 1490 f.a = 3; 1491 i = f.def(); 1492 assert(i == 4); 1493 i = f.Bar!(int).abc(); 1494 assert(i == 3); 1495 1496 Foo61 g; 1497 g.a = 3; 1498 i = g.def(); 1499 assert(i == 4); 1500 i = g.Bar!(int).abc(); 1501 assert(i == 3); 1502 } 1503 1504 /******************************************/ 1505 1506 class Foo62(T) 1507 { 1508 template Bar(T) 1509 { 1510 int func() { return 3; } 1511 } 1512 } 1513 1514 void test62() 1515 { 1516 Foo62!(int) x = new Foo62!(int); 1517 1518 assert(x.Bar!(int).func() == 3); 1519 } 1520 1521 /******************************************/ 1522 1523 class Foo63(T) 1524 { 1525 template Bar(T) 1526 { 1527 int func() { this.def(); return 3; } 1528 int func2() { return 4; } 1529 } 1530 1531 void def() 1532 { 1533 assert(Bar!(T).func2() == 4); 1534 } 1535 } 1536 1537 void test63() 1538 { 1539 Foo63!(int) x = new Foo63!(int); 1540 1541 assert(x.Bar!(int).func() == 3); 1542 x.def(); 1543 } 1544 1545 /******************************************/ 1546 1547 struct XVector(qfloat) 1548 { 1549 qfloat x;qfloat y;qfloat z; 1550 1551 static int opCall (qfloat x, qfloat y, qfloat z) { return 8; } 1552 } 1553 1554 void test64() 1555 { 1556 int i; 1557 i = XVector!(int)(1,2,3); 1558 assert(i == 8); 1559 i = XVector!(real).opCall(1,2,3); 1560 assert(i == 8); 1561 } 1562 1563 /******************************************/ 1564 // http://www.digitalmars.com/d/archives/28052.html 1565 1566 alias int value_type; 1567 1568 struct Foo65 1569 { 1570 uint length() { return 47; } 1571 1572 size_t test() 1573 { 1574 value_type[] e = new value_type[length]; 1575 return e.length; 1576 } 1577 } 1578 1579 void test65() 1580 { 1581 Foo65 f; 1582 1583 assert(f.test() == 47); 1584 } 1585 1586 /******************************************/ 1587 1588 class Thing66 1589 { 1590 template print(T2) 1591 { 1592 void print(T2 t) 1593 { 1594 printf("t = %d\n", t); 1595 assert(t == 10); 1596 } 1597 } 1598 } 1599 1600 1601 void test66() 1602 { 1603 Thing66 thing = new Thing66; 1604 1605 thing.print!(int)(10); 1606 } 1607 1608 /******************************************/ 1609 1610 template Foo67(alias T) 1611 { 1612 void Foo67() 1613 { 1614 printf("T = '%.*s'\n", T.length, T.ptr); 1615 assert(T == "hello"); 1616 } 1617 } 1618 1619 void test67() 1620 { 1621 static string x = "hello"; 1622 1623 Foo67!(x)(); 1624 } 1625 1626 1627 /******************************************/ 1628 1629 template T68(int a) { 1630 int vec[a]; 1631 } 1632 1633 void test68() 1634 { 1635 int i; 1636 1637 i = T68!(4>1?4:1).vec[0]; 1638 assert(i == 0); 1639 i = T68!(4==1?1:(1==1?4:(4>1?1:4))).vec[0]; 1640 assert(i == 0); 1641 } 1642 1643 /******************************************/ 1644 1645 size_t printx(string s) 1646 { 1647 printf("s = '%.*s'\n", s.length, s.ptr); 1648 return s.length; 1649 } 1650 1651 size_t printx(int i) 1652 { 1653 printf("i = %d\n", i); 1654 return 28; 1655 } 1656 1657 template Foo69(alias T) 1658 { 1659 size_t Foo69() 1660 { 1661 return printx(T); 1662 } 1663 } 1664 1665 void test69() 1666 { 1667 static string x = "hello"; 1668 static string z = "abc"; 1669 static int y=100; 1670 size_t i; 1671 1672 alias Foo69!(x) foox; 1673 alias Foo69!(y) fooy; 1674 1675 i = Foo69!(x)(); 1676 assert(i == 5); 1677 i = Foo69!(y)(); 1678 assert(i == 28); 1679 i = Foo69!(z)(); 1680 assert(i == 3); 1681 i = foox(); 1682 assert(i == 5); 1683 i = fooy(); 1684 assert(i == 28); 1685 } 1686 1687 /******************************************/ 1688 1689 template temptt70(alias func) 1690 { 1691 void temp() 1692 { 1693 func(); 1694 } 1695 } 1696 1697 int x70; 1698 1699 void myfunc70() 1700 { 1701 printf("myfunc70()\n"); 1702 x70 = 6; 1703 } 1704 1705 alias temptt70!(myfunc70).temp foo70; 1706 1707 void test70() 1708 { 1709 foo70(); 1710 assert(x70 == 6); 1711 } 1712 1713 /******************************************/ 1714 1715 struct A71(T) 1716 { 1717 alias .A71!(T) AT; 1718 int x; 1719 } 1720 1721 alias A71!(int) Aint71; 1722 1723 void test71() 1724 { 1725 Aint71.AT a; 1726 a.x = 3; 1727 } 1728 1729 /******************************************/ 1730 1731 template foo72(T) 1732 { 1733 char[] foo72(T d) 1734 { 1735 uint sz = typeof(d[0]).sizeof * 2; 1736 return null; 1737 } 1738 } 1739 1740 void test72() 1741 { 1742 static ulong[5] a = [0,1,2,3,4]; 1743 static uint[5] b = [0,1,2,3,4]; 1744 char[] r; 1745 r = foo72!(ulong[5])(a); printf("%.*s\n", r.length, r.ptr); 1746 r = foo72!(uint[5])(b); printf("%.*s\n", r.length, r.ptr); 1747 } 1748 1749 1750 /******************************************/ 1751 1752 alias int Int73; 1753 class Test73(T = Int73); 1754 alias Test73!() Foo73; 1755 1756 void test73() 1757 { 1758 } 1759 1760 /******************************************/ 1761 1762 class A74 1763 { 1764 alias A74 atype; 1765 int x; 1766 } 1767 1768 1769 class B74(R, int V = R.sizeof) 1770 { 1771 int v = V; 1772 } 1773 1774 void test74() 1775 { 1776 B74!(A74,3) b = new B74!(A74,3)(); 1777 assert(b.v == 3); 1778 1779 B74!(A74) c = new B74!(A74)(); 1780 assert(c.v == A74.sizeof); 1781 } 1782 1783 1784 /******************************************/ 1785 1786 interface NotionalRange75(V) 1787 { 1788 } 1789 1790 class MatchedNotionalRange75(R) 1791 : NotionalRange75!(R.value_type) 1792 { 1793 } 1794 1795 class Range75 1796 { 1797 alias int value_type; 1798 } 1799 1800 class List75 1801 { 1802 1803 MatchedNotionalRange75!(Range75) x; 1804 } 1805 1806 void test75() 1807 { 1808 } 1809 1810 1811 /******************************************/ 1812 1813 interface Indian(T) 1814 { 1815 } 1816 1817 interface Iterable(T) 1818 { 1819 Indian!(T) foo(); 1820 } 1821 1822 class Lope(T) : Iterable!(T) 1823 { 1824 Indian!(T) foo() 1825 { 1826 return new Corn!(T); 1827 } 1828 } 1829 1830 class Corn(T) : Indian!(T) 1831 { 1832 } 1833 1834 void test76() 1835 { 1836 Lope!(int) x = new Lope!(int); 1837 } 1838 1839 1840 /******************************************/ 1841 1842 class RawFile 1843 { 1844 } 1845 1846 class Stream : RawFile 1847 { 1848 template readLineT(T) { bool readLineT() 1849 { 1850 if (super) 1851 return false; 1852 return true; 1853 }} 1854 1855 bool readLine() 1856 { 1857 return readLineT!(int)(); 1858 } 1859 } 1860 1861 void test77() 1862 { 1863 } 1864 1865 1866 /******************************************/ 1867 1868 class Four(U, V, X, Y) 1869 { 1870 U i; V j; X k; Y l; 1871 } 1872 1873 template WhatFour(U,V,X,Y) 1874 { 1875 int func(Four!(U,V,X,Y) four) 1876 { 1877 printf("general template\n"); 1878 return 1; 1879 } 1880 } 1881 1882 template WhatFour(U:int,V,X,Y) 1883 { 1884 int func(Four!(int,V,X,Y) four) 1885 { 1886 printf("specialization:: first int\n"); 1887 return 2; 1888 } 1889 } 1890 1891 template WhatFour(U,V:U,X,Y:X) 1892 { 1893 int func(Four!(U,U,X,X) four) 1894 { 1895 printf("specialization:: first two equal, second two equal\n"); 1896 return 3; 1897 } 1898 } 1899 1900 alias WhatFour!(int,float,char,bool).func whatfour; 1901 alias WhatFour!(float,float,char,bool).func whatfour; 1902 alias WhatFour!(float,float,char,char).func whatfour; 1903 alias WhatFour!(int,int,float,char).func whatfour; // ambiguous match 1904 1905 void test78() 1906 { int j; 1907 1908 Four!(int,float,char,bool) f; 1909 Four!(float,float,char,bool) g; 1910 Four!(float,float,char,char) h; 1911 Four!(int,int,float,char) i; 1912 1913 j = whatfour(f); 1914 assert(j == 2); 1915 j = whatfour(g); 1916 assert(j == 1); 1917 j = whatfour(h); 1918 assert(j == 3); 1919 j = whatfour(i); 1920 assert(j == 2); 1921 1922 /* 1923 will print: 1924 specialization:: first int 1925 general template 1926 specialization:: first two equal, second two equal 1927 specialization:: first int 1928 */ 1929 } 1930 1931 1932 /******************************************/ 1933 // http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D.bugs&artnum=2117 1934 1935 class Conversion(T,U){ 1936 alias char Small; 1937 class Big{ 1938 char[2] dummy; 1939 } 1940 static Small Test(U u); 1941 static Big Test(...); 1942 static T MakeT(); 1943 enum { 1944 exists = (Test(MakeT())).sizeof == (Small).sizeof 1945 } 1946 } 1947 1948 void variadicDummy(...){ 1949 } 1950 1951 void test79() 1952 { 1953 variadicDummy(Conversion!(double,int).exists); 1954 } 1955 1956 /******************************************/ 1957 1958 class A80(T) 1959 { 1960 T s; 1961 1962 int foo(int delegate (T) d) { return 3 + d(s); } 1963 1964 int bar() 1965 { 1966 return foo(delegate int (T t) { return 6 + t.x; }); 1967 } 1968 } 1969 1970 class B80: A80!(B80) 1971 { 1972 int x = 20; 1973 } 1974 1975 class C80: A80!(C80) 1976 { 1977 int y = 3; 1978 int x = 10; 1979 } 1980 1981 void test80() 1982 { 1983 B80 b = new B80(); 1984 C80 c = new C80(); 1985 1986 b.s = b; 1987 c.s = c; 1988 1989 assert(b.bar() == 9+20); 1990 assert(c.bar() == 9+10); 1991 } 1992 1993 /******************************************/ 1994 1995 struct T81(FOO) 1996 { 1997 S81 s; 1998 } 1999 2000 struct S81 2001 { 2002 T81!(int)* pt; 2003 } 2004 2005 void test81() 2006 { 2007 } 2008 2009 /******************************************/ 2010 2011 T foo82(T : const(U)*, U=char)(T t) 2012 { 2013 return null; 2014 } 2015 2016 void test82() 2017 { int i; 2018 const int ci; 2019 2020 //writeln(typeid(typeof(foo82(&ci)))); 2021 //writeln(typeid(typeof(foo82(&i)))); 2022 assert(typeof(foo82(&ci)).stringof == "const(int)*"); 2023 assert(typeof(foo82(&i)).stringof == "int*"); 2024 } 2025 2026 /******************************************/ 2027 2028 struct A83 2029 { 2030 void foo(int) {} 2031 void bar(T)(T) {} 2032 } 2033 2034 void test83() 2035 { 2036 A83 a; 2037 a.foo = 5; 2038 a.bar = 6; 2039 } 2040 2041 /******************************************/ 2042 2043 int main() 2044 { 2045 test1(); 2046 test2(); 2047 test3(); 2048 test4(); 2049 test5(); 2050 test6(); 2051 test7(); 2052 test8(); 2053 test9(); 2054 test10(); 2055 test11(); 2056 test12(); 2057 test13(); 2058 test14(); 2059 test15(); 2060 test16(); 2061 test17(); 2062 test18(); 2063 // test19(); 2064 test20(); 2065 test21(); 2066 test22(); 2067 test23(); 2068 test24(); 2069 test25(); 2070 test26(); 2071 test27(); 2072 test28(); 2073 test29(); 2074 test30(); 2075 test31(); 2076 test32(); 2077 test33(); 2078 test34(); 2079 test35(); 2080 test36(); 2081 test37(); 2082 test38(); 2083 test39(); 2084 test40(); 2085 test41(); 2086 test42(); 2087 test43(); 2088 test44(); 2089 test45(); 2090 test46(); 2091 test47(); 2092 test48(); 2093 test49(); 2094 test50(); 2095 test51(); 2096 test52(); 2097 test53(); 2098 test54(); 2099 test55(); 2100 test56(); 2101 test57(); 2102 test58(); 2103 test59(); 2104 test60(); 2105 test61(); 2106 test62(); 2107 test63(); 2108 test64(); 2109 test65(); 2110 test66(); 2111 test67(); 2112 test68(); 2113 test69(); 2114 test70(); 2115 test71(); 2116 test72(); 2117 test73(); 2118 test74(); 2119 test75(); 2120 test76(); 2121 test77(); 2122 test78(); 2123 test79(); 2124 test80(); 2125 test81(); 2126 test82(); 2127 test83(); 2128 2129 printf("Success\n"); 2130 return 0; 2131 }