@@ -634,25 +634,28 @@ $(H3 $(GNAME initSymbol))
634634 $(P Takes a single argument, which must evaluate to a `class`, `struct` or `union` type.
635635 Returns a `const(void)[]` that holds the initial state of any instance of the supplied type.
636636 The slice is constructed for any type `T` as follows:
637+ )
637638
638639 - `ptr` points to either the initializer symbol of `T`
639- or `null` if `T` is a zero-initialized struct / unions .
640+ or `null` if `T` is a $(RELATIVE_LINK2 isZeroInit, zero-initialized) struct/union .
640641
641- - `length` is equal to the size of an instance, i.e. `T.sizeof` for structs / unions and
642- $(RELATIVE_LINK2 classInstanceSize, $(D __traits(classInstanceSize, T)`)) for classes.
643- )
642+ - `length` is equal to the size of an instance, i.e. `T.sizeof` for a struct/union and
643+ $(RELATIVE_LINK2 classInstanceSize, $(D __traits(classInstanceSize, T))) for a class.
644644
645645 $(P
646646 This trait matches the behaviour of `TypeInfo.initializer()` but can also be used when
647647 `TypeInfo` is not available.
648648 )
649649
650650 $(P
651- This traits is not available during $(DDSUBLINK spec/glossary, ctfe, CTFE) because the actual address
651+ This trait is not available during $(DDSUBLINK spec/glossary, ctfe, CTFE) because the actual address
652652 of the initializer symbol will be set by the linker and hence is not available at compile time.
653653 )
654654
655+ $(SPEC_RUNNABLE_EXAMPLE_RUN
655656 ---
657+ import core.stdc.stdlib;
658+
656659 class C
657660 {
658661 int i = 4;
@@ -666,12 +669,14 @@ $(H3 $(GNAME initSymbol))
666669 void* ptr = malloc(initSym.length);
667670 scope (exit) free(ptr);
668671
669- ptr[0..initSym.length] = initSym[];
672+ // Note: allocated memory will only be written to through `c`, so cast is safe
673+ ptr[0..initSym.length] = cast(void[]) initSym[];
670674
671675 C c = cast(C) ptr;
672676 assert(c.i == 4);
673677 }
674678 ---
679+ )
675680
676681
677682$(H2 $(LNAME2 functions, Function Traits))
@@ -694,8 +699,8 @@ static assert(!__traits(isDisabled, Foo.bar));
694699---
695700)
696701
697- $(P For any other declaration even if `@disable` is a syntactically valid
698- attribute `false` is returned because the annotation has no effect.)
702+ $(P For any other declaration, even if `@disable` is a syntactically valid
703+ attribute, `false` is returned because the annotation has no effect.)
699704
700705$(SPEC_RUNNABLE_EXAMPLE_COMPILE
701706---
@@ -1194,6 +1199,25 @@ $(H3 $(GNAME isDeprecated))
11941199 $(P Takes one argument. It returns `true` if the argument is a symbol
11951200 marked with the `deprecated` keyword, otherwise `false`.)
11961201
1202+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
1203+ ---
1204+ deprecated("No longer supported")
1205+ int i;
1206+
1207+ struct A
1208+ {
1209+ int foo() { return 1; }
1210+
1211+ deprecated("please use foo")
1212+ int bar() { return 1; }
1213+ }
1214+
1215+ static assert(__traits(isDeprecated, i));
1216+ static assert(!__traits(isDeprecated, A.foo));
1217+ static assert(__traits(isDeprecated, A.bar));
1218+ ---
1219+ )
1220+
11971221$(H3 $(GNAME isTemplate))
11981222
11991223 $(P Takes one argument. If that argument or any of its overloads is a template
@@ -1203,9 +1227,9 @@ $(H3 $(GNAME isTemplate))
12031227$(SPEC_RUNNABLE_EXAMPLE_COMPILE
12041228---
12051229void foo(T)(){}
1206- static assert(__traits(isTemplate,foo));
1207- static assert(!__traits(isTemplate,foo!int()));
1208- static assert(!__traits(isTemplate,"string"));
1230+ static assert(__traits(isTemplate, foo));
1231+ static assert(!__traits(isTemplate, foo!int()));
1232+ static assert(!__traits(isTemplate, "string"));
12091233---
12101234)
12111235
@@ -1240,12 +1264,14 @@ $(H3 $(GNAME isPackage))
12401264 otherwise $(D false).
12411265 )
12421266
1267+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
12431268---
12441269import std.algorithm.sorting;
12451270static assert(__traits(isPackage, std));
12461271static assert(__traits(isPackage, std.algorithm));
12471272static assert(!__traits(isPackage, std.algorithm.sorting));
12481273---
1274+ )
12491275
12501276$(H3 $(GNAME hasMember))
12511277
@@ -1269,11 +1295,12 @@ void main()
12691295{
12701296 S s;
12711297
1272- writeln(__traits(hasMember, S, "m")); // true
1273- writeln(__traits(hasMember, s, "m")); // true
1274- writeln(__traits(hasMember, S, "y")); // false
1275- writeln(__traits(hasMember, S, "write")); // false, but callable like a member via UFCS
1276- writeln(__traits(hasMember, int, "sizeof")); // true
1298+ static assert(__traits(hasMember, S, "m"));
1299+ static assert(__traits(hasMember, s, "m"));
1300+ static assert(!__traits(hasMember, S, "y"));
1301+ static assert(!__traits(hasMember, S, "write")); // false, but callable like a member via UFCS
1302+ static assert(__traits(hasMember, int, "sizeof"));
1303+ static assert(__traits(hasMember, 5, "sizeof"));
12771304}
12781305---
12791306)
@@ -1283,13 +1310,10 @@ $(H3 $(GNAME identifier))
12831310 $(P Takes one argument, a symbol. Returns the identifier
12841311 for that symbol as a string literal.
12851312 )
1286- $(SPEC_RUNNABLE_EXAMPLE_RUN
1313+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
12871314---
12881315int var = 123;
1289- pragma(msg, typeof(var)); // int
1290- pragma(msg, typeof(__traits(identifier, var))); // string
1291- writeln(var); // 123
1292- writeln(__traits(identifier, var)); // "var"
1316+ static assert(__traits(identifier, var) == "var");
12931317---
12941318)
12951319
@@ -1550,11 +1574,9 @@ $(H3 $(GNAME getUnitTests))
15501574 $(DDSUBLINK spec/attribute, uda, UDAs) will be accessible.
15511575 )
15521576
1553- $(H4 Note:)
1554-
1555- $(P
1556- The -unittest flag needs to be passed to the compiler. If the flag
1557- is not passed $(CODE __traits(getUnitTests)) will always return an
1577+ $(NOTE
1578+ The `-unittest` flag needs to be passed to the compiler. If the flag
1579+ is not passed, $(CODE __traits(getUnitTests)) will always return an
15581580 empty sequence.
15591581 )
15601582
@@ -1678,7 +1700,8 @@ $(H3 $(GNAME allMembers))
16781700 $(P Takes a single argument, which must evaluate to either
16791701 a module, a struct, a union, a class, an interface, an enum, or a
16801702 template instantiation.
1681-
1703+ )
1704+ $(P
16821705 A sequence of string literals is returned, each of which
16831706 is the name of a member of that argument combined with all
16841707 of the members of its base classes (if the argument is a class).
0 commit comments