Changeset 1722
- Timestamp:
- 07/04/10 21:49:57 (14 years ago)
- Files:
-
- trunk/phobos/std/traits.d (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/phobos/std/traits.d
r1709 r1722 831 831 // { 832 832 // struct S1 { char c; int i; } 833 833 // alias RepresentationOffsets!(S1) Offsets; 834 834 // static assert(Offsets[0] == 0); 835 835 // //pragma(msg, Offsets[1]); 836 836 // static assert(Offsets[1] == 4); 837 837 // } 838 838 839 839 // hasRawAliasing 840 840 841 private template HasRawPointerImpl(T...)841 private template hasRawPointerImpl(T...) 842 842 { 843 843 static if (T.length == 0) 844 844 { 845 845 enum result = false; 846 846 } 847 847 else 848 848 { 849 849 static if (is(T[0] foo : U*, U)) 850 850 enum hasRawAliasing = !is(U == immutable); 851 851 else static if (is(T[0] foo : U[], U) && !isStaticArray!(T[0])) 852 852 enum hasRawAliasing = !is(U == immutable); 853 853 else 854 854 enum hasRawAliasing = false; 855 enum result = hasRawAliasing || HasRawPointerImpl!(T[1 .. $]).result;855 enum result = hasRawAliasing || hasRawPointerImpl!(T[1 .. $]).result; 856 856 } 857 857 } 858 858 859 859 private template HasRawLocalPointerImpl(T...) 860 860 { 861 861 static if (T.length == 0) 862 862 { 863 863 enum result = false; 864 864 } 865 865 else … … 899 899 struct S3 { int a; double * b; } 900 900 static assert(hasRawAliasing!(S3)); 901 901 // struct with an indirect pointer member 902 902 struct S4 { S3 a; double b; } 903 903 static assert(hasRawAliasing!(S4)); 904 904 ---- 905 905 */ 906 906 private template hasRawAliasing(T...) 907 907 { 908 908 enum hasRawAliasing 909 = HasRawPointerImpl!(RepresentationTypeTuple!(T)).result;909 = hasRawPointerImpl!(RepresentationTypeTuple!(T)).result; 910 910 } 911 911 912 912 unittest 913 913 { 914 914 // simple types 915 915 static assert(!hasRawAliasing!(int)); 916 916 static assert(hasRawAliasing!(char*)); 917 917 // references aren't raw pointers 918 918 static assert(!hasRawAliasing!(Object)); 919 919 static assert(!hasRawAliasing!(int)); … … 1100 1100 unittest 1101 1101 { 1102 1102 struct S1 { int a; Object b; } 1103 1103 static assert(hasAliasing!(S1)); 1104 1104 struct S2 { string a; } 1105 1105 static assert(!hasAliasing!(S2)); 1106 1106 struct S3 { int a; immutable Object b; } 1107 1107 static assert(!hasAliasing!(S3)); 1108 1108 struct X { float[3] vals; } 1109 1109 static assert(!hasAliasing!X); 1110 } 1111 1112 /** 1113 Returns $(D true) if and only if $(D T)'s representation includes at 1114 least one of the following: $(OL $(LI a raw pointer $(D U*);) $(LI an 1115 array $(D U[]);) $(LI a reference to a class type $(D C).)) 1116 */ 1117 1118 template hasIndirections(T) 1119 { 1120 enum hasIndirections = hasIndirectionsImpl!(RepresentationTypeTuple!T); 1121 } 1122 1123 template hasIndirectionsImpl(T...) 1124 { 1125 static if (!T.length) 1126 { 1127 enum hasIndirectionsImpl = false; 1128 } 1129 else 1130 { 1131 enum hasIndirectionsImpl = isPointer!(T[0]) || isDynamicArray!(T[0]) || 1132 is (T[0] : const(Object)) || hasIndirectionsImpl!(T[1 .. $]); 1133 } 1134 } 1135 1136 unittest 1137 { 1138 struct S1 { int a; Object b; } 1139 static assert(hasIndirections!(S1)); 1140 struct S2 { string a; } 1141 static assert(hasIndirections!(S2)); 1142 struct S3 { int a; immutable Object b; } 1143 static assert(hasIndirections!(S3)); 1110 1144 } 1111 1145 1112 1146 /** 1113 1147 Returns $(D true) if and only if $(D T)'s representation includes at 1114 1148 least one of the following: $(OL $(LI a raw pointer $(D U*) and $(D U) 1115 1149 is not immutable or shared;) $(LI an array $(D U[]) and $(D U) is not 1116 1150 immutable or shared;) $(LI a reference to a class type $(D C) and 1117 1151 $(D C) is not immutable or shared.)) 1118 1152 */ 1119 1153
