Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_traits.d

raw
1 @safe unittest
2 {
3 import std.traits;
4
5 static assert(is(InoutOf!(int) == inout int));
6 static assert(is(InoutOf!(inout int) == inout int));
7 static assert(is(InoutOf!(const int) == inout const int));
8 static assert(is(InoutOf!(shared int) == inout shared int));
9 }
10
11 @safe unittest
12 {
13 import std.traits;
14
15 static assert(is(ConstOf!(int) == const int));
16 static assert(is(ConstOf!(const int) == const int));
17 static assert(is(ConstOf!(inout int) == const inout int));
18 static assert(is(ConstOf!(shared int) == const shared int));
19 }
20
21 @safe unittest
22 {
23 import std.traits;
24
25 static assert(is(SharedOf!(int) == shared int));
26 static assert(is(SharedOf!(shared int) == shared int));
27 static assert(is(SharedOf!(inout int) == shared inout int));
28 static assert(is(SharedOf!(immutable int) == shared immutable int));
29 }
30
31 @safe unittest
32 {
33 import std.traits;
34
35 static assert(is(SharedInoutOf!(int) == shared inout int));
36 static assert(is(SharedInoutOf!(int) == inout shared int));
37
38 static assert(is(SharedInoutOf!(const int) == shared inout const int));
39 static assert(is(SharedInoutOf!(immutable int) == shared inout immutable int));
40 }
41
42 @safe unittest
43 {
44 import std.traits;
45
46 static assert(is(SharedConstOf!(int) == shared const int));
47 static assert(is(SharedConstOf!(int) == const shared int));
48
49 static assert(is(SharedConstOf!(inout int) == shared inout const int));
50 // immutable variables are implicitly shared and const
51 static assert(is(SharedConstOf!(immutable int) == immutable int));
52 }
53
54 @safe unittest
55 {
56 import std.traits;
57
58 static assert(is(SharedConstInoutOf!(int) == shared const inout int));
59 static assert(is(SharedConstInoutOf!(int) == const shared inout int));
60 static assert(is(SharedConstInoutOf!(inout int) == shared inout const int));
61 // immutable variables are implicitly shared and const
62 static assert(is(SharedConstInoutOf!(immutable int) == immutable int));
63 }
64
65 @safe unittest
66 {
67 import std.traits;
68
69 static assert(is(ImmutableOf!(int) == immutable int));
70 static assert(is(ImmutableOf!(const int) == immutable int));
71 static assert(is(ImmutableOf!(inout int) == immutable int));
72 static assert(is(ImmutableOf!(shared int) == immutable int));
73 }
74
75 @safe unittest
76 {
77 import std.traits;
78
79 static assert(__traits(isSame, QualifierOf!(shared const inout int), SharedConstInoutOf));
80 static assert(__traits(isSame, QualifierOf!(immutable int), ImmutableOf));
81 static assert(__traits(isSame, QualifierOf!(shared int), SharedOf));
82 static assert(__traits(isSame, QualifierOf!(shared inout int), SharedInoutOf));
83 import std.meta : Alias;
84 static assert(__traits(isSame, QualifierOf!(int), Alias));
85 }
86
87 @safe unittest
88 {
89 import std.traits;
90
91 static assert(packageName!packageName == "std");
92 }
93
94 @safe unittest
95 {
96 import std.traits;
97
98 static assert(packageName!moduleName == "std");
99 }
100
101 @safe unittest
102 {
103 import std.traits;
104
105 static assert(moduleName!moduleName == "std.traits");
106 }
107
108 @safe unittest
109 {
110 import std.traits;
111
112 static assert(fullyQualifiedName!fullyQualifiedName == "std.traits.fullyQualifiedName");
113 }
114
115 @safe unittest
116 {
117 import std.traits;
118
119 int foo();
120 ReturnType!foo x; // x is declared as int
121 }
122
123 @safe unittest
124 {
125 import std.traits;
126
127 int foo(int, long);
128 void bar(Parameters!foo); // declares void bar(int, long);
129 void abc(Parameters!foo[1]); // declares void abc(long);
130 }
131
132 @safe unittest
133 {
134 import std.traits;
135
136 void foo(){}
137 static assert(arity!foo == 0);
138 void bar(uint){}
139 static assert(arity!bar == 1);
140 void variadicFoo(uint...){}
141 static assert(!__traits(compiles, arity!variadicFoo));
142 }
143
144 @safe unittest
145 {
146 import std.traits;
147
148 alias STC = ParameterStorageClass; // shorten the enum name
149
150 void func(ref int ctx, out real result, in real param, void* ptr)
151 {
152 }
153 alias pstc = ParameterStorageClassTuple!func;
154 static assert(pstc.length == 4); // number of parameters
155 static assert(pstc[0] == STC.ref_);
156 static assert(pstc[1] == STC.out_);
157 version (none)
158 {
159 // TODO: When the DMD PR (dlang/dmd#11474) gets merged,
160 // remove the versioning and the second test
161 static assert(pstc[2] == STC.in_);
162 // This is the current behavior, before `in` is fixed to not be an alias
163 static assert(pstc[2] == STC.scope_);
164 }
165 static assert(pstc[3] == STC.none);
166 }
167
168 @safe unittest
169 {
170 import std.traits;
171
172 static void func(ref int ctx, out real result);
173
174 enum param1 = extractParameterStorageClassFlags!(
175 __traits(getParameterStorageClasses, func, 0)
176 );
177 static assert(param1 == ParameterStorageClass.ref_);
178
179 enum param2 = extractParameterStorageClassFlags!(
180 __traits(getParameterStorageClasses, func, 1)
181 );
182 static assert(param2 == ParameterStorageClass.out_);
183
184 enum param3 = extractParameterStorageClassFlags!(
185 __traits(getParameterStorageClasses, func, 0),
186 __traits(getParameterStorageClasses, func, 1)
187 );
188 static assert(param3 == (ParameterStorageClass.ref_ | ParameterStorageClass.out_));
189 }
190
191 @safe unittest
192 {
193 import std.traits;
194
195 int foo(int num, string name, int);
196 static assert([ParameterIdentifierTuple!foo] == ["num", "name", ""]);
197 }
198
199 @safe unittest
200 {
201 import std.traits;
202
203 int foo(int num, string name = "hello", int[] = [1,2,3], lazy int x = 0);
204 static assert(is(ParameterDefaults!foo[0] == void));
205 static assert( ParameterDefaults!foo[1] == "hello");
206 static assert( ParameterDefaults!foo[2] == [1,2,3]);
207 static assert( ParameterDefaults!foo[3] == 0);
208 }
209
210 @safe unittest
211 {
212 import std.traits;
213
214 alias FA = FunctionAttribute; // shorten the enum name
215
216 real func(real x) pure nothrow @safe
217 {
218 return x;
219 }
220 static assert(functionAttributes!func & FA.pure_);
221 static assert(functionAttributes!func & FA.safe);
222 static assert(!(functionAttributes!func & FA.trusted)); // not @trusted
223 }
224
225 @safe unittest
226 {
227 import std.traits;
228
229 real func(real x) pure nothrow @safe;
230 static assert(hasFunctionAttributes!(func, "@safe", "pure"));
231 static assert(!hasFunctionAttributes!(func, "@trusted"));
232
233 // for templates attributes are automatically inferred
234 bool myFunc(T)(T b)
235 {
236 return !b;
237 }
238 static assert(hasFunctionAttributes!(myFunc!bool, "@safe", "pure", "@nogc", "nothrow"));
239 static assert(!hasFunctionAttributes!(myFunc!bool, "shared"));
240 }
241
242 @safe unittest
243 {
244 import std.traits;
245
246 @safe int add(int a, int b) {return a+b;}
247 @trusted int sub(int a, int b) {return a-b;}
248 @system int mul(int a, int b) {return a*b;}
249
250 static assert( isSafe!add);
251 static assert( isSafe!sub);
252 static assert(!isSafe!mul);
253 }
254
255 @safe unittest
256 {
257 import std.traits;
258
259 @safe int add(int a, int b) {return a+b;}
260 @trusted int sub(int a, int b) {return a-b;}
261 @system int mul(int a, int b) {return a*b;}
262
263 static assert(!isUnsafe!add);
264 static assert(!isUnsafe!sub);
265 static assert( isUnsafe!mul);
266 }
267
268 @safe unittest
269 {
270 import std.traits;
271
272 extern(D) void Dfunc() {}
273 extern(C) void Cfunc() {}
274 static assert(functionLinkage!Dfunc == "D");
275 static assert(functionLinkage!Cfunc == "C");
276
277 string a = functionLinkage!Dfunc;
278 assert(a == "D");
279
280 auto fp = &Cfunc;
281 string b = functionLinkage!fp;
282 assert(b == "C");
283 }
284
285 @safe unittest
286 {
287 import std.traits;
288
289 void func() {}
290 static assert(variadicFunctionStyle!func == Variadic.no);
291
292 extern(C) int printf(const char*, ...);
293 static assert(variadicFunctionStyle!printf == Variadic.c);
294 }
295
296 @safe unittest
297 {
298 import std.traits;
299
300 class C
301 {
302 int value() @property => 0;
303 static string opCall() => "hi";
304 }
305 static assert(is( typeof(C.value) == int ));
306 static assert(is( FunctionTypeOf!(C.value) == function ));
307 static assert(is( FunctionTypeOf!C == typeof(C.opCall) ));
308
309 int function() fp;
310 alias IntFn = int();
311 static assert(is( typeof(fp) == IntFn* ));
312 static assert(is( FunctionTypeOf!fp == IntFn ));
313 }
314
315 @safe unittest
316 {
317 import std.traits;
318
319 alias ExternC(T) = SetFunctionAttributes!(T, "C", functionAttributes!T);
320
321 auto assumePure(T)(T t)
322 if (isFunctionPointer!T || isDelegate!T)
323 {
324 enum attrs = functionAttributes!T | FunctionAttribute.pure_;
325 return cast(SetFunctionAttributes!(T, functionLinkage!T, attrs)) t;
326 }
327
328 int f()
329 {
330 import core.thread : getpid;
331 return getpid();
332 }
333
334 int g() pure @trusted
335 {
336 auto pureF = assumePure(&f);
337 return pureF();
338 }
339 assert(g() > 0);
340 }
341
342 @safe unittest
343 {
344 import std.traits;
345
346 class C
347 {
348 int outer;
349 }
350 static assert(!isInnerClass!C);
351
352 class Outer1
353 {
354 class Inner1 { }
355 class Inner2
356 {
357 int outer;
358 }
359 }
360 static assert(isInnerClass!(Outer1.Inner1));
361 static assert(!isInnerClass!(Outer1.Inner2));
362
363 static class Outer2
364 {
365 static class Inner
366 {
367 int outer;
368 }
369 }
370 static assert(!isInnerClass!(Outer2.Inner));
371 }
372
373 @safe unittest
374 {
375 import std.traits;
376
377 static struct S { }
378 static assert(!isNested!S);
379
380 int i;
381 struct NestedStruct { void f() { ++i; } }
382 static assert(isNested!NestedStruct);
383 }
384
385 @safe unittest
386 {
387 import std.traits;
388
389 static struct S { }
390
391 int i;
392 struct NS { void f() { ++i; } }
393
394 static assert(!hasNested!(S[2]));
395 static assert(hasNested!(NS[2]));
396 }
397
398 @safe unittest
399 {
400 import std.traits;
401
402 import std.meta : AliasSeq;
403 struct S { int x; float y; }
404 static assert(is(Fields!S == AliasSeq!(int, float)));
405 }
406
407 @safe unittest
408 {
409 import std.traits;
410
411 import std.meta : AliasSeq;
412 struct S { int x; float y; }
413 static assert(FieldNameTuple!S == AliasSeq!("x", "y"));
414 static assert(FieldNameTuple!int == AliasSeq!"");
415 }
416
417 @safe unittest
418 {
419 import std.traits;
420
421 struct S1 { int a; float b; }
422 struct S2 { char[] a; union { S1 b; S1 * c; } }
423 alias R = RepresentationTypeTuple!S2;
424 assert(R.length == 4
425 && is(R[0] == char[]) && is(R[1] == int)
426 && is(R[2] == float) && is(R[3] == S1*));
427 }
428
429 @safe unittest
430 {
431 import std.traits;
432
433 struct S1 { int a; Object b; }
434 struct S2 { string a; }
435 struct S3 { int a; immutable Object b; }
436 struct S4 { float[3] vals; }
437 static assert( hasAliasing!S1);
438 static assert(!hasAliasing!S2);
439 static assert(!hasAliasing!S3);
440 static assert(!hasAliasing!S4);
441 }
442
443 @safe unittest
444 {
445 import std.traits;
446
447 static assert( hasIndirections!(int[string]));
448 static assert( hasIndirections!(void delegate()));
449 static assert( hasIndirections!(void delegate() immutable));
450 static assert( hasIndirections!(immutable(void delegate())));
451 static assert( hasIndirections!(immutable(void delegate() immutable)));
452
453 static assert(!hasIndirections!(void function()));
454 static assert( hasIndirections!(void*[1]));
455 static assert(!hasIndirections!(byte[1]));
456 }
457
458 @safe unittest
459 {
460 import std.traits;
461
462 struct S1 { int a; Object b; }
463 struct S2 { string a; }
464 struct S3 { int a; immutable Object b; }
465 static assert( hasUnsharedAliasing!S1);
466 static assert(!hasUnsharedAliasing!S2);
467 static assert(!hasUnsharedAliasing!S3);
468
469 struct S4 { int a; shared Object b; }
470 struct S5 { char[] a; }
471 struct S6 { shared char[] b; }
472 struct S7 { float[3] vals; }
473 static assert(!hasUnsharedAliasing!S4);
474 static assert( hasUnsharedAliasing!S5);
475 static assert(!hasUnsharedAliasing!S6);
476 static assert(!hasUnsharedAliasing!S7);
477 }
478
479 @safe unittest
480 {
481 import std.traits;
482
483 static assert(!hasElaborateCopyConstructor!int);
484
485 static struct S1 { }
486 static struct S2 { this(this) {} }
487 static struct S3 { S2 field; }
488 static struct S4 { S3[1] field; }
489 static struct S5 { S3[] field; }
490 static struct S6 { S3[0] field; }
491 static struct S7 { @disable this(); S3 field; }
492 static assert(!hasElaborateCopyConstructor!S1);
493 static assert( hasElaborateCopyConstructor!S2);
494 static assert( hasElaborateCopyConstructor!(immutable S2));
495 static assert( hasElaborateCopyConstructor!S3);
496 static assert( hasElaborateCopyConstructor!(S3[1]));
497 static assert(!hasElaborateCopyConstructor!(S3[0]));
498 static assert( hasElaborateCopyConstructor!S4);
499 static assert(!hasElaborateCopyConstructor!S5);
500 static assert(!hasElaborateCopyConstructor!S6);
501 static assert( hasElaborateCopyConstructor!S7);
502 }
503
504 @safe unittest
505 {
506 import std.traits;
507
508 static assert(!hasElaborateAssign!int);
509
510 static struct S { void opAssign(S) {} }
511 static assert( hasElaborateAssign!S);
512 static assert(!hasElaborateAssign!(const(S)));
513
514 static struct S1 { void opAssign(ref S1) {} }
515 static struct S2 { void opAssign(int) {} }
516 static struct S3 { S s; }
517 static assert( hasElaborateAssign!S1);
518 static assert(!hasElaborateAssign!S2);
519 static assert( hasElaborateAssign!S3);
520 static assert( hasElaborateAssign!(S3[1]));
521 static assert(!hasElaborateAssign!(S3[0]));
522 }
523
524 @safe unittest
525 {
526 import std.traits;
527
528 static assert(!hasElaborateDestructor!int);
529
530 static struct S1 { }
531 static struct S2 { ~this() {} }
532 static struct S3 { S2 field; }
533 static struct S4 { S3[1] field; }
534 static struct S5 { S3[] field; }
535 static struct S6 { S3[0] field; }
536 static struct S7 { @disable this(); S3 field; }
537 static assert(!hasElaborateDestructor!S1);
538 static assert( hasElaborateDestructor!S2);
539 static assert( hasElaborateDestructor!(immutable S2));
540 static assert( hasElaborateDestructor!S3);
541 static assert( hasElaborateDestructor!(S3[1]));
542 static assert(!hasElaborateDestructor!(S3[0]));
543 static assert( hasElaborateDestructor!S4);
544 static assert(!hasElaborateDestructor!S5);
545 static assert(!hasElaborateDestructor!S6);
546 static assert( hasElaborateDestructor!S7);
547 }
548
549 @safe unittest
550 {
551 import std.traits;
552
553 static assert(!hasElaborateMove!int);
554
555 static struct S1 { }
556 static struct S2 { void opPostMove(ref S2) {} }
557 static struct S3 { void opPostMove(inout ref S3) inout {} }
558 static struct S4 { void opPostMove(const ref S4) {} }
559 static struct S5 { void opPostMove(S5) {} }
560 static struct S6 { void opPostMove(int) {} }
561 static struct S7 { S3[1] field; }
562 static struct S8 { S3[] field; }
563 static struct S9 { S3[0] field; }
564 static struct S10 { @disable this(); S3 field; }
565 static assert(!hasElaborateMove!S1);
566 static assert( hasElaborateMove!S2);
567 static assert( hasElaborateMove!S3);
568 static assert( hasElaborateMove!(immutable S3));
569 static assert( hasElaborateMove!S4);
570 static assert(!hasElaborateMove!S5);
571 static assert(!hasElaborateMove!S6);
572 static assert( hasElaborateMove!S7);
573 static assert(!hasElaborateMove!S8);
574 static assert(!hasElaborateMove!S9);
575 static assert( hasElaborateMove!S10);
576 }
577
578 @safe unittest
579 {
580 import std.traits;
581
582 static assert(!hasMember!(int, "blah"));
583 struct S1 { int blah; }
584 struct S2 { int blah(){ return 0; } }
585 class C1 { int blah; }
586 class C2 { int blah(){ return 0; } }
587 static assert(hasMember!(S1, "blah"));
588 static assert(hasMember!(S2, "blah"));
589 static assert(hasMember!(C1, "blah"));
590 static assert(hasMember!(C2, "blah"));
591 }
592
593 @safe unittest
594 {
595 import std.traits;
596
597 static struct S
598 {
599 static void sf() {}
600 void f() {}
601
602 static int si;
603 int i;
604 }
605
606 static assert( hasStaticMember!(S, "sf"));
607 static assert(!hasStaticMember!(S, "f"));
608
609 static assert( hasStaticMember!(S, "si"));
610 static assert(!hasStaticMember!(S, "i"));
611
612 static assert(!hasStaticMember!(S, "hello"));
613 }
614
615 @safe unittest
616 {
617 import std.traits;
618
619 enum Sqrts : real
620 {
621 one = 1,
622 two = 1.41421,
623 three = 1.73205
624 }
625 auto sqrts = [EnumMembers!Sqrts];
626 assert(sqrts == [Sqrts.one, Sqrts.two, Sqrts.three]);
627 }
628
629 @safe unittest
630 {
631 import std.traits;
632
633 // Returns i if e is the i-th enumerator of E.
634 static size_t rank(E)(E e)
635 if (is(E == enum))
636 {
637 static foreach (i, member; EnumMembers!E)
638 {
639 if (e == member)
640 return i;
641 }
642 assert(0, "Not an enum member");
643 }
644
645 enum Mode
646 {
647 read = 1,
648 write = 2,
649 map = 4
650 }
651 assert(rank(Mode.read) == 0);
652 assert(rank(Mode.write) == 1);
653 assert(rank(Mode.map) == 2);
654 }
655
656 @safe unittest
657 {
658 import std.traits;
659
660 import std.conv : to;
661 class FooClass
662 {
663 string calledMethod;
664 void foo() @safe { calledMethod = "foo"; }
665 void bar() @safe { calledMethod = "bar"; }
666 void baz() @safe { calledMethod = "baz"; }
667 }
668
669 enum FooEnum { foo, bar, baz }
670
671 auto var = FooEnum.bar;
672 auto fooObj = new FooClass();
673 s: final switch (var)
674 {
675 static foreach (member; EnumMembers!FooEnum)
676 {
677 case member: // Generate a case for each enum value.
678 // Call fooObj.{name of enum value}().
679 __traits(getMember, fooObj, to!string(member))();
680 break s;
681 }
682 }
683 // As we pass in FooEnum.bar, the bar() method gets called.
684 assert(fooObj.calledMethod == "bar");
685 }
686
687 @safe unittest
688 {
689 import std.traits;
690
691 import std.meta : AliasSeq;
692
693 interface I1 { }
694 interface I2 { }
695 interface I12 : I1, I2 { }
696 static assert(is(BaseTypeTuple!I12 == AliasSeq!(I1, I2)));
697
698 interface I3 : I1 { }
699 interface I123 : I1, I2, I3 { }
700 static assert(is(BaseTypeTuple!I123 == AliasSeq!(I1, I2, I3)));
701 }
702
703 @safe unittest
704 {
705 import std.traits;
706
707 import std.meta : AliasSeq;
708
709 class C1 { }
710 class C2 : C1 { }
711 class C3 : C2 { }
712 static assert(!BaseClassesTuple!Object.length);
713 static assert(is(BaseClassesTuple!C1 == AliasSeq!(Object)));
714 static assert(is(BaseClassesTuple!C2 == AliasSeq!(C1, Object)));
715 static assert(is(BaseClassesTuple!C3 == AliasSeq!(C2, C1, Object)));
716 }
717
718 @safe unittest
719 {
720 import std.traits;
721
722 interface I1 {}
723 interface I2 {}
724 class A : I1, I2 {}
725 class B : A, I1 {}
726 class C : B {}
727
728 alias TL = InterfacesTuple!C;
729 static assert(is(TL[0] == I1) && is(TL[1] == I2));
730 }
731
732 @safe unittest
733 {
734 import std.traits;
735
736 interface J1 {}
737 interface J2 {}
738 class B1 {}
739 class B2 : B1, J1, J2 {}
740 class B3 : B2, J1 {}
741 alias TL = TransitiveBaseTypeTuple!B3;
742 assert(TL.length == 5);
743 assert(is (TL[0] == B2));
744 assert(is (TL[1] == B1));
745 assert(is (TL[2] == Object));
746 assert(is (TL[3] == J1));
747 assert(is (TL[4] == J2));
748
749 assert(TransitiveBaseTypeTuple!Object.length == 0);
750 }
751
752 @safe unittest
753 {
754 import std.traits;
755
756 interface I { I foo(); }
757 class B
758 {
759 real foo(real v) { return v; }
760 }
761 class C : B, I
762 {
763 override C foo() { return this; } // covariant overriding of I.foo()
764 }
765 alias foos = MemberFunctionsTuple!(C, "foo");
766 static assert(foos.length == 2);
767 static assert(__traits(isSame, foos[0], C.foo));
768 static assert(__traits(isSame, foos[1], B.foo));
769 }
770
771 @safe unittest
772 {
773 import std.traits;
774
775 struct Foo(T, U) {}
776 static assert(__traits(isSame, TemplateOf!(Foo!(int, real)), Foo));
777 }
778
779 @safe unittest
780 {
781 import std.traits;
782
783 import std.meta : AliasSeq;
784
785 struct Foo(T, U) {}
786 static assert(is(TemplateArgsOf!(Foo!(int, real)) == AliasSeq!(int, real)));
787 }
788
789 @safe unittest
790 {
791 import std.traits;
792
793 class A { byte b; }
794 class B { long l; }
795
796 // As class instance always has a hidden pointer
797 static assert(classInstanceAlignment!A == (void*).alignof);
798 static assert(classInstanceAlignment!B == long.alignof);
799 }
800
801 @safe unittest
802 {
803 import std.traits;
804
805 alias X = CommonType!(int, long, short);
806 assert(is(X == long));
807 alias Y = CommonType!(int, char[], short);
808 assert(is(Y == void));
809 }
810
811 @safe unittest
812 {
813 import std.traits;
814
815 static assert(is(CommonType!(3) == int));
816 static assert(is(CommonType!(double, 4, float) == double));
817 static assert(is(CommonType!(string, char[]) == const(char)[]));
818 static assert(is(CommonType!(3, 3U) == uint));
819 static assert(is(CommonType!(double, int) == double));
820 }
821
822 @safe unittest
823 {
824 import std.traits;
825
826 import std.meta : AliasSeq;
827
828 static assert(is(AllImplicitConversionTargets!(ulong) == AliasSeq!(long, float, double, real)));
829 static assert(is(AllImplicitConversionTargets!(int) == AliasSeq!(dchar, uint, long, ulong, float, double, real)));
830 static assert(is(AllImplicitConversionTargets!(float) == AliasSeq!(double, real)));
831 static assert(is(AllImplicitConversionTargets!(double) == AliasSeq!(float, real)));
832
833 static assert(is(AllImplicitConversionTargets!(char) ==
834 AliasSeq!(byte, ubyte, short, ushort, wchar, int, dchar, uint, long,
835 ulong, float, double, real)
836 ));
837 static assert(is(AllImplicitConversionTargets!(wchar) == AliasSeq!(
838 short, ushort, dchar, int, uint, long, ulong, float, double, real
839 )));
840 static assert(is(AllImplicitConversionTargets!(dchar) == AliasSeq!(
841 int, uint, long, ulong, float, double, real
842 )));
843
844 static assert(is(AllImplicitConversionTargets!(string) == AliasSeq!(const(char)[])));
845 static assert(is(AllImplicitConversionTargets!(int*) == AliasSeq!(void*)));
846
847 interface A {}
848 interface B {}
849 class C : A, B {}
850
851 static assert(is(AllImplicitConversionTargets!(C) == AliasSeq!(Object, A, B)));
852 static assert(is(AllImplicitConversionTargets!(const C) == AliasSeq!(const Object, const A, const B)));
853 static assert(is(AllImplicitConversionTargets!(immutable C) == AliasSeq!(
854 immutable Object, immutable A, immutable B
855 )));
856
857 interface I : A, B {}
858
859 static assert(is(AllImplicitConversionTargets!(I) == AliasSeq!(A, B)));
860 static assert(is(AllImplicitConversionTargets!(const I) == AliasSeq!(const A, const B)));
861 static assert(is(AllImplicitConversionTargets!(immutable I) == AliasSeq!(
862 immutable A, immutable B
863 )));
864 }
865
866 @safe unittest
867 {
868 import std.traits;
869
870 static assert( isImplicitlyConvertible!(immutable(char), char));
871 static assert( isImplicitlyConvertible!(const(char), char));
872 static assert( isImplicitlyConvertible!(char, wchar));
873 static assert(!isImplicitlyConvertible!(wchar, char));
874
875 static assert(!isImplicitlyConvertible!(const(ushort), ubyte));
876 static assert(!isImplicitlyConvertible!(const(uint), ubyte));
877 static assert(!isImplicitlyConvertible!(const(ulong), ubyte));
878
879 static assert(!isImplicitlyConvertible!(const(char)[], string));
880 static assert( isImplicitlyConvertible!(string, const(char)[]));
881 }
882
883 @safe unittest
884 {
885 import std.traits;
886
887 // Mutable and immmutable both convert to const...
888 static assert( isQualifierConvertible!(char, const(char)));
889 static assert( isQualifierConvertible!(immutable(char), const(char)));
890 // ...but const does not convert back to mutable or immutable
891 static assert(!isQualifierConvertible!(const(char), char));
892 static assert(!isQualifierConvertible!(const(char), immutable(char)));
893 }
894
895 @safe unittest
896 {
897 import std.traits;
898
899 static assert( isAssignable!(long, int));
900 static assert(!isAssignable!(int, long));
901 static assert( isAssignable!(const(char)[], string));
902 static assert(!isAssignable!(string, char[]));
903
904 // int is assignable to int
905 static assert( isAssignable!int);
906
907 // immutable int is not assignable to immutable int
908 static assert(!isAssignable!(immutable int));
909 }
910
911 @safe unittest
912 {
913 import std.traits;
914
915 struct S1
916 {
917 void opAssign(S1);
918 }
919
920 struct S2
921 {
922 void opAssign(ref S2);
923 }
924
925 static assert( isRvalueAssignable!(long, int));
926 static assert(!isRvalueAssignable!(int, long));
927 static assert( isRvalueAssignable!S1);
928 static assert(!isRvalueAssignable!S2);
929 }
930
931 @safe unittest
932 {
933 import std.traits;
934
935 struct S1
936 {
937 void opAssign(S1);
938 }
939
940 struct S2
941 {
942 void opAssign(ref S2);
943 }
944
945 static assert( isLvalueAssignable!(long, int));
946 static assert(!isLvalueAssignable!(int, long));
947 static assert( isLvalueAssignable!S1);
948 static assert( isLvalueAssignable!S2);
949 }
950
951 @safe unittest
952 {
953 import std.traits;
954
955 interface I { I clone(); }
956 interface J { J clone(); }
957 class C : I
958 {
959 override C clone() // covariant overriding of I.clone()
960 {
961 return new C;
962 }
963 }
964
965 // C.clone() can override I.clone(), indeed.
966 static assert(isCovariantWith!(typeof(C.clone), typeof(I.clone)));
967
968 // C.clone() can't override J.clone(); the return type C is not implicitly
969 // convertible to J.
970 static assert(!isCovariantWith!(typeof(C.clone), typeof(J.clone)));
971 }
972
973 @system unittest
974 {
975 import std.traits;
976
977 static int f(int);
978 static assert(is(typeof(f(rvalueOf!int)) == int));
979 }
980
981 @system unittest
982 {
983 import std.traits;
984
985 static bool f(ref int);
986 static assert(is(typeof(f(lvalueOf!int)) == bool));
987 }
988
989 @safe unittest
990 {
991 import std.traits;
992
993 static assert( isBoolean!bool);
994 enum EB : bool { a = true }
995 static assert( isBoolean!EB);
996
997 struct SubTypeOfBool
998 {
999 bool val;
1000 alias val this;
1001 }
1002 static assert(!isBoolean!(SubTypeOfBool));
1003 }
1004
1005 @safe unittest
1006 {
1007 import std.traits;
1008
1009 static assert(
1010 isIntegral!byte &&
1011 isIntegral!short &&
1012 isIntegral!int &&
1013 isIntegral!long &&
1014 isIntegral!(const(long)) &&
1015 isIntegral!(immutable(long))
1016 );
1017
1018 static assert(
1019 !isIntegral!bool &&
1020 !isIntegral!char &&
1021 !isIntegral!double
1022 );
1023
1024 // types which act as integral values do not pass
1025 struct S
1026 {
1027 int val;
1028 alias val this;
1029 }
1030
1031 static assert(!isIntegral!S);
1032 }
1033
1034 @safe unittest
1035 {
1036 import std.traits;
1037
1038 static assert(
1039 isFloatingPoint!float &&
1040 isFloatingPoint!double &&
1041 isFloatingPoint!real &&
1042 isFloatingPoint!(const(real)) &&
1043 isFloatingPoint!(immutable(real))
1044 );
1045
1046 static assert(!isFloatingPoint!int);
1047
1048 // types which act as floating point values do not pass
1049 struct S
1050 {
1051 float val;
1052 alias val this;
1053 }
1054
1055 static assert(!isFloatingPoint!S);
1056 }
1057
1058 @safe unittest
1059 {
1060 import std.traits;
1061
1062 static assert(
1063 isNumeric!byte &&
1064 isNumeric!short &&
1065 isNumeric!int &&
1066 isNumeric!long &&
1067 isNumeric!float &&
1068 isNumeric!double &&
1069 isNumeric!real &&
1070 isNumeric!(const(real)) &&
1071 isNumeric!(immutable(real))
1072 );
1073
1074 static assert(
1075 !isNumeric!void &&
1076 !isNumeric!bool &&
1077 !isNumeric!char &&
1078 !isNumeric!wchar &&
1079 !isNumeric!dchar
1080 );
1081
1082 // types which act as numeric values do not pass
1083 struct S
1084 {
1085 int val;
1086 alias val this;
1087 }
1088
1089 static assert(!isNumeric!S);
1090 }
1091
1092 @safe unittest
1093 {
1094 import std.traits;
1095
1096 static assert(!isScalarType!void);
1097 static assert( isScalarType!(immutable(byte)));
1098 static assert( isScalarType!(immutable(ushort)));
1099 static assert( isScalarType!(immutable(int)));
1100 static assert( isScalarType!(ulong));
1101 static assert( isScalarType!(shared(float)));
1102 static assert( isScalarType!(shared(const bool)));
1103 static assert( isScalarType!(const(char)));
1104 static assert( isScalarType!(wchar));
1105 static assert( isScalarType!(const(dchar)));
1106 static assert( isScalarType!(const(double)));
1107 static assert( isScalarType!(const(real)));
1108 }
1109
1110 @safe unittest
1111 {
1112 import std.traits;
1113
1114 static assert(isBasicType!void);
1115 static assert(isBasicType!(const(void)));
1116 static assert(isBasicType!(shared(void)));
1117 static assert(isBasicType!(immutable(void)));
1118 static assert(isBasicType!(shared const(void)));
1119 static assert(isBasicType!(shared inout(void)));
1120 static assert(isBasicType!(shared inout const(void)));
1121 static assert(isBasicType!(inout(void)));
1122 static assert(isBasicType!(inout const(void)));
1123 static assert(isBasicType!(immutable(int)));
1124 static assert(isBasicType!(shared(float)));
1125 static assert(isBasicType!(shared(const bool)));
1126 static assert(isBasicType!(const(dchar)));
1127 }
1128
1129 @safe unittest
1130 {
1131 import std.traits;
1132
1133 static assert(
1134 isUnsigned!uint &&
1135 isUnsigned!ulong
1136 );
1137
1138 static assert(
1139 !isUnsigned!char &&
1140 !isUnsigned!int &&
1141 !isUnsigned!long &&
1142 !isUnsigned!char &&
1143 !isUnsigned!wchar &&
1144 !isUnsigned!dchar
1145 );
1146 }
1147
1148 @safe unittest
1149 {
1150 import std.traits;
1151
1152 static assert(
1153 isSigned!int &&
1154 isSigned!long
1155 );
1156
1157 static assert(
1158 !isSigned!uint &&
1159 !isSigned!ulong
1160 );
1161 }
1162
1163 @safe unittest
1164 {
1165 import std.traits;
1166
1167 //Char types
1168 static assert( isSomeChar!char);
1169 static assert( isSomeChar!wchar);
1170 static assert( isSomeChar!dchar);
1171 static assert( isSomeChar!(typeof('c')));
1172 static assert( isSomeChar!(immutable char));
1173 static assert( isSomeChar!(const dchar));
1174
1175 //Non char types
1176 static assert(!isSomeChar!int);
1177 static assert(!isSomeChar!byte);
1178 static assert(!isSomeChar!string);
1179 static assert(!isSomeChar!wstring);
1180 static assert(!isSomeChar!dstring);
1181 static assert(!isSomeChar!(char[4]));
1182 }
1183
1184 @safe unittest
1185 {
1186 import std.traits;
1187
1188 //String types
1189 static assert( isSomeString!string);
1190 static assert( isSomeString!(wchar[]));
1191 static assert( isSomeString!(dchar[]));
1192 static assert( isSomeString!(typeof("aaa")));
1193 static assert( isSomeString!(const(char)[]));
1194
1195 //Non string types
1196 static assert(!isSomeString!int);
1197 static assert(!isSomeString!(int[]));
1198 static assert(!isSomeString!(byte[]));
1199 static assert(!isSomeString!(typeof(null)));
1200 static assert(!isSomeString!(char[4]));
1201
1202 enum ES : string { a = "aaa", b = "bbb" }
1203 static assert(!isSomeString!ES);
1204
1205 static struct Stringish
1206 {
1207 string str;
1208 alias str this;
1209 }
1210 static assert(!isSomeString!Stringish);
1211 }
1212
1213 @safe unittest
1214 {
1215 import std.traits;
1216
1217 static assert(isNarrowString!string);
1218 static assert(isNarrowString!wstring);
1219 static assert(isNarrowString!(char[]));
1220 static assert(isNarrowString!(wchar[]));
1221
1222 static assert(!isNarrowString!dstring);
1223 static assert(!isNarrowString!(dchar[]));
1224
1225 static assert(!isNarrowString!(typeof(null)));
1226 static assert(!isNarrowString!(char[4]));
1227
1228 enum ES : string { a = "aaa", b = "bbb" }
1229 static assert(!isNarrowString!ES);
1230
1231 static struct Stringish
1232 {
1233 string str;
1234 alias str this;
1235 }
1236 static assert(!isNarrowString!Stringish);
1237 }
1238
1239 @safe unittest
1240 {
1241 import std.traits;
1242
1243 static assert(isOrderingComparable!int);
1244 static assert(isOrderingComparable!string);
1245
1246 static struct Foo {}
1247 static assert(!isOrderingComparable!Foo);
1248
1249 static struct Bar
1250 {
1251 int a;
1252 auto opCmp(Bar b1) const { return a - b1.a; }
1253 }
1254
1255 Bar b1 = Bar(5);
1256 Bar b2 = Bar(7);
1257 assert(isOrderingComparable!Bar && b2 > b1);
1258 }
1259
1260 @safe unittest
1261 {
1262 import std.traits;
1263
1264 static struct AliasedString
1265 {
1266 string s;
1267 alias s this;
1268 }
1269
1270 enum StringEnum { a = "foo" }
1271
1272 assert(!isConvertibleToString!string);
1273 assert(isConvertibleToString!AliasedString);
1274 assert(isConvertibleToString!StringEnum);
1275 assert(isConvertibleToString!(char[25]));
1276 assert(!isConvertibleToString!(char[]));
1277 }
1278
1279 @safe unittest
1280 {
1281 import std.traits;
1282
1283 static struct Stringish
1284 {
1285 string s;
1286 alias s this;
1287 }
1288 static assert(isAutodecodableString!wstring);
1289 static assert(isAutodecodableString!Stringish);
1290 static assert(!isAutodecodableString!dstring);
1291
1292 enum E : const(char)[3] { X = "abc" }
1293 enum F : const(char)[] { X = "abc" }
1294 enum G : F { X = F.init }
1295
1296 static assert(isAutodecodableString!(char[]));
1297 static assert(!isAutodecodableString!(E));
1298 static assert(isAutodecodableString!(F));
1299 static assert(isAutodecodableString!(G));
1300
1301 struct Stringish2
1302 {
1303 Stringish s;
1304 alias s this;
1305 }
1306
1307 enum H : Stringish { X = Stringish() }
1308 enum I : Stringish2 { X = Stringish2() }
1309
1310 static assert(isAutodecodableString!(H));
1311 static assert(isAutodecodableString!(I));
1312
1313 static assert(!isAutodecodableString!(noreturn[]));
1314 static assert(!isAutodecodableString!(immutable(noreturn)[]));
1315 }
1316
1317 @safe unittest
1318 {
1319 import std.traits;
1320
1321 static assert( isStaticArray!(int[3]));
1322 static assert( isStaticArray!(const(int)[5]));
1323 static assert( isStaticArray!(const(int)[][5]));
1324
1325 static assert(!isStaticArray!(const(int)[]));
1326 static assert(!isStaticArray!(immutable(int)[]));
1327 static assert(!isStaticArray!(const(int)[4][]));
1328 static assert(!isStaticArray!(int[]));
1329 static assert(!isStaticArray!(int[char]));
1330 static assert(!isStaticArray!(int[1][]));
1331 static assert(!isStaticArray!(int[int]));
1332 static assert(!isStaticArray!int);
1333 }
1334
1335 @safe unittest
1336 {
1337 import std.traits;
1338
1339 static assert( isDynamicArray!(int[]));
1340 static assert( isDynamicArray!(string));
1341 static assert( isDynamicArray!(long[3][]));
1342
1343 static assert(!isDynamicArray!(int[5]));
1344 static assert(!isDynamicArray!(typeof(null)));
1345 }
1346
1347 @safe unittest
1348 {
1349 import std.traits;
1350
1351 static assert( isArray!(int[]));
1352 static assert( isArray!(int[5]));
1353 static assert( isArray!(string));
1354
1355 static assert(!isArray!uint);
1356 static assert(!isArray!(uint[uint]));
1357 static assert(!isArray!(typeof(null)));
1358 }
1359
1360 @safe unittest
1361 {
1362 import std.traits;
1363
1364 struct S;
1365
1366 static assert( isAssociativeArray!(int[string]));
1367 static assert( isAssociativeArray!(S[S]));
1368 static assert(!isAssociativeArray!(string[]));
1369 static assert(!isAssociativeArray!S);
1370 static assert(!isAssociativeArray!(int[4]));
1371 }
1372
1373 @safe unittest
1374 {
1375 import std.traits;
1376
1377 class C;
1378 union U;
1379 struct S;
1380 interface I;
1381
1382 static assert( isBuiltinType!void);
1383 static assert( isBuiltinType!string);
1384 static assert( isBuiltinType!(int[]));
1385 static assert( isBuiltinType!(C[string]));
1386 static assert( isBuiltinType!(typeof(null)));
1387 static assert(!isBuiltinType!C);
1388 static assert(!isBuiltinType!U);
1389 static assert(!isBuiltinType!S);
1390 static assert(!isBuiltinType!I);
1391 static assert(!isBuiltinType!(void delegate(int)));
1392 }
1393
1394 @safe unittest
1395 {
1396 import std.traits;
1397
1398 static if (is(__vector(float[4])))
1399 {
1400 alias SimdVec = __vector(float[4]);
1401 static assert(isSIMDVector!(__vector(float[4])));
1402 static assert(isSIMDVector!SimdVec);
1403 }
1404 static assert(!isSIMDVector!uint);
1405 static assert(!isSIMDVector!(float[4]));
1406 }
1407
1408 @safe unittest
1409 {
1410 import std.traits;
1411
1412 void fun();
1413
1414 static assert( isPointer!(int*));
1415 static assert( isPointer!(int function()));
1416 static assert(!isPointer!int);
1417 static assert(!isPointer!string);
1418 static assert(!isPointer!(typeof(null)));
1419 static assert(!isPointer!(typeof(fun)));
1420 static assert(!isPointer!(int delegate()));
1421 }
1422
1423 @safe unittest
1424 {
1425 import std.traits;
1426
1427 static assert(is(PointerTarget!(int*) == int));
1428 static assert(is(PointerTarget!(void*) == void));
1429 }
1430
1431 @safe unittest
1432 {
1433 import std.traits;
1434
1435 class C {}
1436 union U {}
1437 struct S {}
1438 interface I {}
1439
1440 static assert( isAggregateType!C);
1441 static assert( isAggregateType!U);
1442 static assert( isAggregateType!S);
1443 static assert( isAggregateType!I);
1444 static assert(!isAggregateType!void);
1445 static assert(!isAggregateType!string);
1446 static assert(!isAggregateType!(int[]));
1447 static assert(!isAggregateType!(C[string]));
1448 static assert(!isAggregateType!(void delegate(int)));
1449
1450 enum ES : S { a = S.init }
1451 enum EC : C { a = C.init }
1452 enum EI : I { a = I.init }
1453 enum EU : U { a = U.init }
1454
1455 static assert( isAggregateType!ES);
1456 static assert( isAggregateType!EC);
1457 static assert( isAggregateType!EI);
1458 static assert( isAggregateType!EU);
1459 }
1460
1461 @safe unittest
1462 {
1463 import std.traits;
1464
1465 struct OpApply
1466 {
1467 int opApply(scope int delegate(ref uint) dg) { assert(0); }
1468 }
1469
1470 struct Range
1471 {
1472 @property uint front() { assert(0); }
1473 void popFront() { assert(0); }
1474 enum bool empty = false;
1475 }
1476
1477 static assert( isIterable!(uint[]));
1478 static assert( isIterable!OpApply);
1479 static assert( isIterable!(uint[string]));
1480 static assert( isIterable!Range);
1481
1482 static assert(!isIterable!uint);
1483 }
1484
1485 @safe unittest
1486 {
1487 import std.traits;
1488
1489 static assert( isMutable!int);
1490 static assert( isMutable!string);
1491 static assert( isMutable!(shared int));
1492 static assert( isMutable!(shared const(int)[]));
1493
1494 static assert(!isMutable!(const int));
1495 static assert(!isMutable!(inout int));
1496 static assert(!isMutable!(shared(const int)));
1497 static assert(!isMutable!(shared(inout int)));
1498 static assert(!isMutable!(immutable string));
1499 }
1500
1501 @safe unittest
1502 {
1503 import std.traits;
1504
1505 static struct Foo(T...) { }
1506 static struct Bar(T...) { }
1507 static struct Doo(T) { }
1508 static struct ABC(int x) { }
1509 static void fun(T)() { }
1510 template templ(T) { }
1511
1512 static assert(isInstanceOf!(Foo, Foo!int));
1513 static assert(!isInstanceOf!(Foo, Bar!int));
1514 static assert(!isInstanceOf!(Foo, int));
1515 static assert(isInstanceOf!(Doo, Doo!int));
1516 static assert(isInstanceOf!(ABC, ABC!1));
1517 static assert(!isInstanceOf!(Foo, Foo));
1518 static assert(isInstanceOf!(fun, fun!int));
1519 static assert(isInstanceOf!(templ, templ!int));
1520 }
1521
1522 @safe unittest
1523 {
1524 import std.traits;
1525
1526 static struct A(T = void)
1527 {
1528 // doesn't work as expected, only accepts A when T = void
1529 void func(B)(B b)
1530 if (isInstanceOf!(A, B)) {}
1531
1532 // correct behavior
1533 void method(B)(B b)
1534 if (isInstanceOf!(TemplateOf!(A), B)) {}
1535 }
1536
1537 A!(void) a1;
1538 A!(void) a2;
1539 A!(int) a3;
1540
1541 static assert(!__traits(compiles, a1.func(a3)));
1542 static assert( __traits(compiles, a1.method(a2)));
1543 static assert( __traits(compiles, a1.method(a3)));
1544 }
1545
1546 @safe unittest
1547 {
1548 import std.traits;
1549
1550 static assert(isExpressions!(1, 2.0, "a"));
1551 static assert(!isExpressions!(int, double, string));
1552 static assert(!isExpressions!(int, 2.0, "a"));
1553 }
1554
1555 @safe unittest
1556 {
1557 import std.traits;
1558
1559 static assert(isTypeTuple!(int, float, string));
1560 static assert(!isTypeTuple!(1, 2.0, "a"));
1561 static assert(!isTypeTuple!(1, double, string));
1562 }
1563
1564 @safe unittest
1565 {
1566 import std.traits;
1567
1568 static void foo() {}
1569 void bar() {}
1570
1571 auto fpfoo = &foo;
1572 static assert( isFunctionPointer!fpfoo);
1573 static assert( isFunctionPointer!(void function()));
1574
1575 auto dgbar = &bar;
1576 static assert(!isFunctionPointer!dgbar);
1577 static assert(!isFunctionPointer!(void delegate()));
1578 static assert(!isFunctionPointer!foo);
1579 static assert(!isFunctionPointer!bar);
1580
1581 static assert( isFunctionPointer!((int a) {}));
1582 }
1583
1584 @safe unittest
1585 {
1586 import std.traits;
1587
1588 static void sfunc() { }
1589 int x;
1590 void func() { x++; }
1591
1592 int delegate() dg;
1593 assert(isDelegate!dg);
1594 assert(isDelegate!(int delegate()));
1595 assert(isDelegate!(typeof(&func)));
1596
1597 int function() fp;
1598 assert(!isDelegate!fp);
1599 assert(!isDelegate!(int function()));
1600 assert(!isDelegate!(typeof(&sfunc)));
1601 }
1602
1603 @safe unittest
1604 {
1605 import std.traits;
1606
1607 static real func(ref int) { return 0; }
1608 static void prop() @property { }
1609 class C
1610 {
1611 real method(ref int) { return 0; }
1612 real prop() @property { return 0; }
1613 }
1614 auto c = new C;
1615 auto fp = &func;
1616 auto dg = &c.method;
1617
1618 static assert( isSomeFunction!func);
1619 static assert( isSomeFunction!prop);
1620 static assert( isSomeFunction!(C.method));
1621 static assert( isSomeFunction!(C.prop));
1622 static assert( isSomeFunction!(c.prop));
1623 static assert( isSomeFunction!fp);
1624 static assert( isSomeFunction!dg);
1625
1626 real val;
1627 static assert(!isSomeFunction!int);
1628 static assert(!isSomeFunction!val);
1629 }
1630
1631 @safe unittest
1632 {
1633 import std.traits;
1634
1635 void f() { }
1636 int g(int x) { return x; }
1637
1638 static assert( isCallable!f);
1639 static assert( isCallable!g);
1640
1641 class C { int opCall(int) { return 0; } }
1642 auto c = new C;
1643 struct S { static int opCall(int) { return 0; } }
1644 interface I { real value() @property; }
1645
1646 static assert( isCallable!c);
1647 static assert( isCallable!(c.opCall));
1648 static assert( isCallable!S);
1649 static assert( isCallable!(I.value));
1650 static assert( isCallable!((int a) { return a; }));
1651
1652 static assert(!isCallable!I);
1653 }
1654
1655 @safe unittest
1656 {
1657 import std.traits;
1658
1659 void f()() { }
1660 T g(T = int)(T x) { return x; }
1661 struct S1 { static void opCall()() { } }
1662 struct S2 { static T opCall(T = int)(T x) {return x; } }
1663
1664 static assert( isCallable!f);
1665 static assert( isCallable!g);
1666 static assert( isCallable!S1);
1667 static assert( isCallable!S2);
1668 }
1669
1670 @safe unittest
1671 {
1672 import std.traits;
1673
1674 static struct Wrapper
1675 {
1676 void f() { }
1677 int f(int x) { return x; }
1678
1679 void g()() { }
1680 T g(T = int)(T x) { return x; }
1681 }
1682
1683 static assert(isCallable!(Wrapper.f));
1684 static assert(isCallable!(Wrapper.g));
1685 }
1686
1687 @safe unittest
1688 {
1689 import std.traits;
1690
1691 struct S { void foo() { } }
1692 class C { void foo() { } }
1693 class AC { abstract void foo(); }
1694 static assert(!isAbstractFunction!(int));
1695 static assert(!isAbstractFunction!(S.foo));
1696 static assert(!isAbstractFunction!(C.foo));
1697 static assert( isAbstractFunction!(AC.foo));
1698 }
1699
1700 @safe unittest
1701 {
1702 import std.traits;
1703
1704 struct S { void bar() { } }
1705 final class FC { void foo(); }
1706 class C
1707 {
1708 void bar() { }
1709 final void foo();
1710 }
1711 static assert(!isFinalFunction!(int));
1712 static assert(!isFinalFunction!(S.bar));
1713 static assert( isFinalFunction!(FC.foo));
1714 static assert(!isFinalFunction!(C.bar));
1715 static assert( isFinalFunction!(C.foo));
1716 }
1717
1718 @safe unittest
1719 {
1720 import std.traits;
1721
1722 static void f() {}
1723 static void fun()
1724 {
1725 int i;
1726 int f() { return i; }
1727
1728 static assert(isNestedFunction!(f));
1729 }
1730
1731 static assert(!isNestedFunction!f);
1732 }
1733
1734 @safe unittest
1735 {
1736 import std.traits;
1737
1738 struct S { }
1739 class C { }
1740 abstract class AC { }
1741 static assert(!isAbstractClass!S);
1742 static assert(!isAbstractClass!C);
1743 static assert( isAbstractClass!AC);
1744 C c;
1745 static assert(!isAbstractClass!c);
1746 AC ac;
1747 static assert( isAbstractClass!ac);
1748 }
1749
1750 @safe unittest
1751 {
1752 import std.traits;
1753
1754 class C { }
1755 abstract class AC { }
1756 final class FC1 : C { }
1757 final class FC2 { }
1758 static assert(!isFinalClass!C);
1759 static assert(!isFinalClass!AC);
1760 static assert( isFinalClass!FC1);
1761 static assert( isFinalClass!FC2);
1762 C c;
1763 static assert(!isFinalClass!c);
1764 FC1 fc1;
1765 static assert( isFinalClass!fc1);
1766 }
1767
1768 @safe unittest
1769 {
1770 import std.traits;
1771
1772 static assert(is(Unconst!int == int));
1773 static assert(is(Unconst!(const int) == int));
1774 static assert(is(Unconst!(immutable int) == int));
1775 static assert(is(Unconst!(shared int) == shared int));
1776 static assert(is(Unconst!(shared(const int)) == shared int));
1777 }
1778
1779 @safe unittest
1780 {
1781 import std.traits;
1782
1783 static assert(is(Unshared!int == int));
1784 static assert(is(Unshared!(const int) == const int));
1785 static assert(is(Unshared!(immutable int) == immutable int));
1786
1787 static assert(is(Unshared!(shared int) == int));
1788 static assert(is(Unshared!(shared(const int)) == const int));
1789
1790 static assert(is(Unshared!(shared(int[])) == shared(int)[]));
1791 }
1792
1793 @safe unittest
1794 {
1795 import std.traits;
1796
1797 static assert(is(Unqual!int == int));
1798 static assert(is(Unqual!(const int) == int));
1799 static assert(is(Unqual!(immutable int) == int));
1800 static assert(is(Unqual!(shared int) == int));
1801 static assert(is(Unqual!(shared(const int)) == int));
1802 }
1803
1804 @safe unittest
1805 {
1806 import std.traits;
1807
1808 static assert(is(CopyTypeQualifiers!(inout const real, int) == inout const int));
1809 }
1810
1811 @safe unittest
1812 {
1813 import std.traits;
1814
1815 const(int) i;
1816 CopyConstness!(typeof(i), float) f;
1817 assert( is(typeof(f) == const float));
1818
1819 CopyConstness!(char, uint) u;
1820 assert( is(typeof(u) == uint));
1821
1822 //The 'shared' qualifier will not be copied
1823 assert(!is(CopyConstness!(shared bool, int) == shared int));
1824
1825 //But the constness will be
1826 assert( is(CopyConstness!(shared const real, double) == const double));
1827
1828 //Careful, const(int)[] is a mutable array of const(int)
1829 alias MutT = CopyConstness!(const(int)[], int);
1830 assert(!is(MutT == const(int)));
1831
1832 //Okay, const(int[]) applies to array and contained ints
1833 alias CstT = CopyConstness!(const(int[]), int);
1834 assert( is(CstT == const(int)));
1835 }
1836
1837 @safe unittest
1838 {
1839 import std.traits;
1840
1841 static assert(is(ForeachType!(uint[]) == uint));
1842 static assert(is(ForeachType!string == immutable(char)));
1843 static assert(is(ForeachType!(string[string]) == string));
1844 static assert(is(ForeachType!(inout(int)[]) == inout(int)));
1845 }
1846
1847 @safe unittest
1848 {
1849 import std.traits;
1850
1851 enum E : real { a = 0 } // NOTE: explicit initialization to 0 required during Enum init deprecation cycle
1852 enum F : E { a = E.a }
1853 alias G = const(F);
1854 static assert(is(OriginalType!E == real));
1855 static assert(is(OriginalType!F == real));
1856 static assert(is(OriginalType!G == const real));
1857 }
1858
1859 @safe unittest
1860 {
1861 import std.traits;
1862
1863 alias Hash = int[string];
1864 static assert(is(KeyType!Hash == string));
1865 static assert(is(ValueType!Hash == int));
1866 KeyType!Hash str = "a"; // str is declared as string
1867 ValueType!Hash num = 1; // num is declared as int
1868 }
1869
1870 @safe unittest
1871 {
1872 import std.traits;
1873
1874 alias Hash = int[string];
1875 static assert(is(KeyType!Hash == string));
1876 static assert(is(ValueType!Hash == int));
1877 KeyType!Hash str = "a"; // str is declared as string
1878 ValueType!Hash num = 1; // num is declared as int
1879 }
1880
1881 @safe unittest
1882 {
1883 import std.traits;
1884
1885 static assert(is(Unsigned!(int) == uint));
1886 static assert(is(Unsigned!(long) == ulong));
1887 static assert(is(Unsigned!(const short) == const ushort));
1888 static assert(is(Unsigned!(immutable byte) == immutable ubyte));
1889 static assert(is(Unsigned!(inout int) == inout uint));
1890 }
1891
1892 @safe unittest
1893 {
1894 import std.traits;
1895
1896 static assert(is(Unsigned!(uint) == uint));
1897 static assert(is(Unsigned!(const uint) == const uint));
1898
1899 static assert(is(Unsigned!(ubyte) == ubyte));
1900 static assert(is(Unsigned!(immutable uint) == immutable uint));
1901 }
1902
1903 @safe unittest
1904 {
1905 import std.traits;
1906
1907 static assert(is(Largest!(uint, ubyte, ushort, real) == real));
1908 static assert(is(Largest!(ulong, double) == ulong));
1909 static assert(is(Largest!(double, ulong) == double));
1910 static assert(is(Largest!(uint, byte, double, short) == double));
1911 static if (is(ucent))
1912 static assert(is(Largest!(uint, ubyte, ucent, ushort) == ucent));
1913 }
1914
1915 @safe unittest
1916 {
1917 import std.traits;
1918
1919 alias S1 = Signed!uint;
1920 static assert(is(S1 == int));
1921 alias S2 = Signed!(const(uint));
1922 static assert(is(S2 == const(int)));
1923 alias S3 = Signed!(immutable(uint));
1924 static assert(is(S3 == immutable(int)));
1925 static if (is(ucent))
1926 {
1927 alias S4 = Signed!ucent;
1928 static assert(is(S4 == cent));
1929 }
1930 }
1931
1932 @safe unittest
1933 {
1934 import std.traits;
1935
1936 static assert(mostNegative!float == -float.max);
1937 static assert(mostNegative!double == -double.max);
1938 static assert(mostNegative!real == -real.max);
1939 static assert(mostNegative!bool == false);
1940 }
1941
1942 @safe unittest
1943 {
1944 import std.traits;
1945
1946 import std.meta : AliasSeq;
1947
1948 static foreach (T; AliasSeq!(bool, byte, short, int, long))
1949 static assert(mostNegative!T == T.min);
1950
1951 static foreach (T; AliasSeq!(ubyte, ushort, uint, ulong, char, wchar, dchar))
1952 static assert(mostNegative!T == 0);
1953 }
1954
1955 @safe unittest
1956 {
1957 import std.traits;
1958
1959 ubyte a = 3, b = 5;
1960 static assert(is(typeof(a * b) == Promoted!ubyte));
1961 static assert(is(Promoted!ubyte == int));
1962
1963 static assert(is(Promoted!(shared(bool)) == shared(int)));
1964 static assert(is(Promoted!(const(int)) == const(int)));
1965 static assert(is(Promoted!double == double));
1966 }
1967
1968 @safe unittest
1969 {
1970 import std.traits;
1971
1972 import std.meta : AliasSeq;
1973 alias TL = staticMap!(mangledName, int, const int, immutable int);
1974 static assert(TL == AliasSeq!("i", "xi", "yi"));
1975 }
1976
1977 @safe unittest
1978 {
1979 import std.traits;
1980
1981 // can select types
1982 static assert(is(Select!(true, int, long) == int));
1983 static assert(is(Select!(false, int, long) == long));
1984 static struct Foo {}
1985 static assert(is(Select!(false, const(int), const(Foo)) == const(Foo)));
1986
1987 // can select symbols
1988 int a = 1;
1989 int b = 2;
1990 alias selA = Select!(true, a, b);
1991 alias selB = Select!(false, a, b);
1992 assert(selA == 1);
1993 assert(selB == 2);
1994
1995 // can select (compile-time) expressions
1996 enum val = Select!(false, -4, 9 - 6);
1997 static assert(val == 3);
1998 }
1999
2000 @safe unittest
2001 {
2002 import std.traits;
2003
2004 real run() { return 0; }
2005 int fail() { assert(0); }
2006 auto a = select!true(run(), fail());
2007 auto b = select!false(fail(), run());
2008 static assert(is(typeof(a) == real));
2009 static assert(is(typeof(b) == real));
2010 }
2011
2012 @safe unittest
2013 {
2014 import std.traits;
2015
2016 enum E;
2017 struct S {}
2018
2019 @("alpha") int a;
2020 static assert(hasUDA!(a, "alpha"));
2021 static assert(!hasUDA!(a, S));
2022 static assert(!hasUDA!(a, E));
2023
2024 @(E) int b;
2025 static assert(!hasUDA!(b, "alpha"));
2026 static assert(!hasUDA!(b, S));
2027 static assert(hasUDA!(b, E));
2028
2029 @E int c;
2030 static assert(!hasUDA!(c, "alpha"));
2031 static assert(!hasUDA!(c, S));
2032 static assert(hasUDA!(c, E));
2033
2034 @(S, E) int d;
2035 static assert(!hasUDA!(d, "alpha"));
2036 static assert(hasUDA!(d, S));
2037 static assert(hasUDA!(d, E));
2038
2039 @S int e;
2040 static assert(!hasUDA!(e, "alpha"));
2041 static assert(hasUDA!(e, S));
2042 static assert(!hasUDA!(e, S()));
2043 static assert(!hasUDA!(e, E));
2044
2045 @S() int f;
2046 static assert(!hasUDA!(f, "alpha"));
2047 static assert(hasUDA!(f, S));
2048 static assert(hasUDA!(f, S()));
2049 static assert(!hasUDA!(f, E));
2050
2051 @(S, E, "alpha") int g;
2052 static assert(hasUDA!(g, "alpha"));
2053 static assert(hasUDA!(g, S));
2054 static assert(hasUDA!(g, E));
2055
2056 @(100) int h;
2057 static assert(hasUDA!(h, 100));
2058
2059 struct Named { string name; }
2060
2061 @Named("abc") int i;
2062 static assert(hasUDA!(i, Named));
2063 static assert(hasUDA!(i, Named("abc")));
2064 static assert(!hasUDA!(i, Named("def")));
2065
2066 struct AttrT(T)
2067 {
2068 string name;
2069 T value;
2070 }
2071
2072 @AttrT!int("answer", 42) int j;
2073 static assert(hasUDA!(j, AttrT));
2074 static assert(hasUDA!(j, AttrT!int));
2075 static assert(!hasUDA!(j, AttrT!string));
2076
2077 @AttrT!string("hello", "world") int k;
2078 static assert(hasUDA!(k, AttrT));
2079 static assert(!hasUDA!(k, AttrT!int));
2080 static assert(hasUDA!(k, AttrT!string));
2081
2082 struct FuncAttr(alias f) { alias func = f; }
2083 static int fourtyTwo() { return 42; }
2084 static size_t getLen(string s) { return s.length; }
2085
2086 @FuncAttr!getLen int l;
2087 static assert(hasUDA!(l, FuncAttr));
2088 static assert(!hasUDA!(l, FuncAttr!fourtyTwo));
2089 static assert(hasUDA!(l, FuncAttr!getLen));
2090 static assert(!hasUDA!(l, FuncAttr!fourtyTwo()));
2091 static assert(!hasUDA!(l, FuncAttr!getLen()));
2092
2093 @FuncAttr!getLen() int m;
2094 static assert(hasUDA!(m, FuncAttr));
2095 static assert(!hasUDA!(m, FuncAttr!fourtyTwo));
2096 static assert(hasUDA!(m, FuncAttr!getLen));
2097 static assert(!hasUDA!(m, FuncAttr!fourtyTwo()));
2098 static assert(hasUDA!(m, FuncAttr!getLen()));
2099 }
2100
2101 @safe unittest
2102 {
2103 import std.traits;
2104
2105 struct Attr
2106 {
2107 string name;
2108 int value;
2109 }
2110
2111 @Attr("Answer", 42) int a;
2112 static assert(getUDAs!(a, Attr).length == 1);
2113 static assert(getUDAs!(a, Attr)[0].name == "Answer");
2114 static assert(getUDAs!(a, Attr)[0].value == 42);
2115
2116 @(Attr("Answer", 42), "string", 9999) int b;
2117 static assert(getUDAs!(b, Attr).length == 1);
2118 static assert(getUDAs!(b, Attr)[0].name == "Answer");
2119 static assert(getUDAs!(b, Attr)[0].value == 42);
2120
2121 @Attr("Answer", 42) @Attr("Pi", 3) int c;
2122 static assert(getUDAs!(c, Attr).length == 2);
2123 static assert(getUDAs!(c, Attr)[0].name == "Answer");
2124 static assert(getUDAs!(c, Attr)[0].value == 42);
2125 static assert(getUDAs!(c, Attr)[1].name == "Pi");
2126 static assert(getUDAs!(c, Attr)[1].value == 3);
2127
2128 static assert(getUDAs!(c, Attr("Answer", 42)).length == 1);
2129 static assert(getUDAs!(c, Attr("Answer", 42))[0].name == "Answer");
2130 static assert(getUDAs!(c, Attr("Answer", 42))[0].value == 42);
2131
2132 static assert(getUDAs!(c, Attr("Answer", 99)).length == 0);
2133
2134 struct AttrT(T)
2135 {
2136 string name;
2137 T value;
2138 }
2139
2140 @AttrT!uint("Answer", 42) @AttrT!int("Pi", 3) @AttrT int d;
2141 static assert(getUDAs!(d, AttrT).length == 2);
2142 static assert(getUDAs!(d, AttrT)[0].name == "Answer");
2143 static assert(getUDAs!(d, AttrT)[0].value == 42);
2144 static assert(getUDAs!(d, AttrT)[1].name == "Pi");
2145 static assert(getUDAs!(d, AttrT)[1].value == 3);
2146
2147 static assert(getUDAs!(d, AttrT!uint).length == 1);
2148 static assert(getUDAs!(d, AttrT!uint)[0].name == "Answer");
2149 static assert(getUDAs!(d, AttrT!uint)[0].value == 42);
2150
2151 static assert(getUDAs!(d, AttrT!int).length == 1);
2152 static assert(getUDAs!(d, AttrT!int)[0].name == "Pi");
2153 static assert(getUDAs!(d, AttrT!int)[0].value == 3);
2154
2155 struct SimpleAttr {}
2156
2157 @SimpleAttr int e;
2158 static assert(getUDAs!(e, SimpleAttr).length == 1);
2159 static assert(is(getUDAs!(e, SimpleAttr)[0] == SimpleAttr));
2160
2161 @SimpleAttr() int f;
2162 static assert(getUDAs!(f, SimpleAttr).length == 1);
2163 static assert(is(typeof(getUDAs!(f, SimpleAttr)[0]) == SimpleAttr));
2164
2165 struct FuncAttr(alias f) { alias func = f; }
2166 static int add42(int v) { return v + 42; }
2167 static string concat(string l, string r) { return l ~ r; }
2168
2169 @FuncAttr!add42 int g;
2170 static assert(getUDAs!(g, FuncAttr).length == 1);
2171 static assert(getUDAs!(g, FuncAttr)[0].func(5) == 47);
2172
2173 static assert(getUDAs!(g, FuncAttr!add42).length == 1);
2174 static assert(getUDAs!(g, FuncAttr!add42)[0].func(5) == 47);
2175
2176 static assert(getUDAs!(g, FuncAttr!add42()).length == 0);
2177
2178 static assert(getUDAs!(g, FuncAttr!concat).length == 0);
2179 static assert(getUDAs!(g, FuncAttr!concat()).length == 0);
2180
2181 @FuncAttr!add42() int h;
2182 static assert(getUDAs!(h, FuncAttr).length == 1);
2183 static assert(getUDAs!(h, FuncAttr)[0].func(5) == 47);
2184
2185 static assert(getUDAs!(h, FuncAttr!add42).length == 1);
2186 static assert(getUDAs!(h, FuncAttr!add42)[0].func(5) == 47);
2187
2188 static assert(getUDAs!(h, FuncAttr!add42()).length == 1);
2189 static assert(getUDAs!(h, FuncAttr!add42())[0].func(5) == 47);
2190
2191 static assert(getUDAs!(h, FuncAttr!concat).length == 0);
2192 static assert(getUDAs!(h, FuncAttr!concat()).length == 0);
2193
2194 @("alpha") @(42) int i;
2195 static assert(getUDAs!(i, "alpha").length == 1);
2196 static assert(getUDAs!(i, "alpha")[0] == "alpha");
2197
2198 static assert(getUDAs!(i, 42).length == 1);
2199 static assert(getUDAs!(i, 42)[0] == 42);
2200
2201 static assert(getUDAs!(i, 'c').length == 0);
2202 }
2203
2204 @safe unittest
2205 {
2206 import std.traits;
2207
2208 enum Attr;
2209 struct A
2210 {
2211 @Attr int a;
2212 int b;
2213 }
2214
2215 static assert(getSymbolsByUDA!(A, Attr).length == 1);
2216 static assert(hasUDA!(getSymbolsByUDA!(A, Attr)[0], Attr));
2217 }
2218
2219 @safe unittest
2220 {
2221 import std.traits;
2222
2223 enum Attr;
2224
2225 static struct A
2226 {
2227 @Attr int a;
2228 int b;
2229 @Attr void doStuff() {}
2230 void doOtherStuff() {}
2231 static struct Inner
2232 {
2233 // Not found by getSymbolsByUDA
2234 @Attr int c;
2235 }
2236 }
2237
2238 // Finds both variables and functions with the attribute, but
2239 // doesn't include the variables and functions without it.
2240 static assert(getSymbolsByUDA!(A, Attr).length == 2);
2241 // Can access attributes on the symbols returned by getSymbolsByUDA.
2242 static assert(hasUDA!(getSymbolsByUDA!(A, Attr)[0], Attr));
2243 static assert(hasUDA!(getSymbolsByUDA!(A, Attr)[1], Attr));
2244 }
2245
2246 @safe unittest
2247 {
2248 import std.traits;
2249
2250 static struct UDA { string name; }
2251
2252 static struct B
2253 {
2254 @UDA("X")
2255 int x;
2256 @UDA("Y")
2257 int y;
2258 @(100)
2259 int z;
2260 }
2261
2262 // Finds both UDA attributes.
2263 static assert(getSymbolsByUDA!(B, UDA).length == 2);
2264 // Finds one `100` attribute.
2265 static assert(getSymbolsByUDA!(B, 100).length == 1);
2266 // Can get the value of the UDA from the return value
2267 static assert(getUDAs!(getSymbolsByUDA!(B, UDA)[0], UDA)[0].name == "X");
2268 }
2269
2270 @safe unittest
2271 {
2272 import std.traits;
2273
2274 static struct UDA { string name; }
2275
2276 @UDA("A")
2277 static struct C
2278 {
2279 @UDA("B")
2280 int d;
2281 }
2282
2283 static assert(getSymbolsByUDA!(C, UDA).length == 2);
2284 static assert(getSymbolsByUDA!(C, UDA)[0].stringof == "C");
2285 static assert(getSymbolsByUDA!(C, UDA)[1].stringof == "d");
2286 }
2287
2288 @safe unittest
2289 {
2290 import std.traits;
2291
2292 static struct UDA { string name; }
2293
2294 static struct D
2295 {
2296 int x;
2297 }
2298
2299 static assert(getSymbolsByUDA!(D, UDA).length == 0);
2300 }
2301
2302 @safe unittest
2303 {
2304 import std.traits;
2305
2306 static assert(allSameType!());
2307 static assert(allSameType!(int));
2308 static assert(allSameType!(int, int));
2309 static assert(allSameType!(int, int, int));
2310 static assert(allSameType!(float, float, float));
2311 static assert(!allSameType!(int, double));
2312 static assert(!allSameType!(int, float, double));
2313 static assert(!allSameType!(int, float, double, real));
2314 static assert(!allSameType!(short, int, float, double, real));
2315 }
2316
2317 @safe unittest
2318 {
2319 import std.traits;
2320
2321 class C;
2322 struct S1;
2323 struct S2
2324 {
2325 T opCast(T)() const;
2326 }
2327
2328 static assert( ifTestable!bool);
2329 static assert( ifTestable!int);
2330 static assert( ifTestable!(S1*));
2331 static assert( ifTestable!(typeof(null)));
2332 static assert( ifTestable!(int[]));
2333 static assert( ifTestable!(int[string]));
2334 static assert( ifTestable!S2);
2335 static assert( ifTestable!C);
2336 static assert(!ifTestable!S1);
2337 }
2338
2339 @safe unittest
2340 {
2341 import std.traits;
2342
2343 struct S {
2344 template Test() {}
2345 }
2346 class C {}
2347 interface I {}
2348 union U {}
2349 static assert(isType!int);
2350 static assert(isType!string);
2351 static assert(isType!(int[int]));
2352 static assert(isType!S);
2353 static assert(isType!C);
2354 static assert(isType!I);
2355 static assert(isType!U);
2356
2357 int n;
2358 void func(){}
2359 static assert(!isType!n);
2360 static assert(!isType!func);
2361 static assert(!isType!(S.Test));
2362 static assert(!isType!(S.Test!()));
2363 }
2364
2365 @safe unittest
2366 {
2367 import std.traits;
2368
2369 static void func(){}
2370 static assert(isFunction!func);
2371
2372 struct S
2373 {
2374 void func(){}
2375 }
2376 static assert(isFunction!(S.func));
2377 }
2378
2379 @safe unittest
2380 {
2381 import std.traits;
2382
2383 class C
2384 {
2385 void nf() {}
2386 static void sf() {}
2387 final void ff() {}
2388 }
2389 final class FC { }
2390
2391 static assert(!isFinal!(C));
2392 static assert( isFinal!(FC));
2393
2394 static assert(!isFinal!(C.nf));
2395 static assert(!isFinal!(C.sf));
2396 static assert( isFinal!(C.ff));
2397 }
2398
2399 @safe unittest
2400 {
2401 import std.traits;
2402
2403 struct S1 {} // Fine. Can be copied
2404 struct S2 { this(this) {}} // Fine. Can be copied
2405 struct S3 {@disable this(this); } // Not fine. Copying is disabled.
2406 struct S4 {S3 s;} // Not fine. A field has copying disabled.
2407
2408 class C1 {}
2409
2410 static assert( isCopyable!S1);
2411 static assert( isCopyable!S2);
2412 static assert(!isCopyable!S3);
2413 static assert(!isCopyable!S4);
2414
2415 static assert(isCopyable!C1);
2416 static assert(isCopyable!int);
2417 static assert(isCopyable!(int[]));
2418 }
2419