@@ -1547,13 +1547,38 @@ $(H4 $(LNAME2 cast_floating, Floating Point))
1547
1547
1548
1548
$(H4 $(LNAME2 cast_struct, Structs))
1549
1549
1550
- $(P Casting an expression $(I e) to a struct $(I S), when the
1551
- expression is not a struct of the same type, is equivalent to a
1552
- $(GLINK PostfixExpression):)
1550
+ $(P An expression `e` can be cast to a struct type `S`:)
1553
1551
1552
+ * The compiler attempts a
1553
+ $(RELATIVE_LINK2 type-constructor-arguments, *PostfixExpression*) `S(e)`.
1554
+ If that would fail:
1555
+ * When `e` is a struct or static array instance, its data is reinterpreted as
1556
+ the target type `S`. The data sizes must match.
1557
+ * Otherwise, it is an error.
1558
+
1559
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1554
1560
---
1555
- S(e)
1561
+ struct S
1562
+ {
1563
+ int i;
1564
+ }
1565
+ struct R
1566
+ {
1567
+ short[2] a;
1568
+ }
1569
+
1570
+ S s = cast(S) 5; // same as S(5)
1571
+ assert(s.i == 5);
1572
+ static assert(!__traits(compiles, cast(S) long.max)); // S(long.max) is invalid
1573
+
1574
+ R r = R([1, 2]);
1575
+ s = cast(S) r; // reinterpret r
1576
+ assert(s.i == 0x00020001);
1577
+
1578
+ byte[4] a = [1, 0, 2, 0];
1579
+ assert(r == cast(R) a); // reinterpret a
1556
1580
---
1581
+ )
1557
1582
1558
1583
$(P A struct instance can be cast to a static array type when
1559
1584
their `.sizeof` properties each give the same result.)
@@ -1563,7 +1588,7 @@ $(H4 $(LNAME2 cast_struct, Structs))
1563
1588
struct S { short a, b, c; }
1564
1589
1565
1590
S s = S(1, 2, 3);
1566
- static assert(!__traits(compiles, cast(short[2]) s));
1591
+ static assert(!__traits(compiles, cast(short[2]) s)); // size mismatch
1567
1592
1568
1593
short[3] x = cast(short[3]) s;
1569
1594
assert(x.tupleof == s.tupleof);
@@ -1775,6 +1800,7 @@ $(H4 $(LNAME2 type-constructor-arguments, Constructing a Type with an Argument L
1775
1800
$(P A type can precede a list of arguments. See:)
1776
1801
1777
1802
* $(DDSUBLINK spec/struct, struct-literal, Struct Literals)
1803
+ * $(DDSUBLINK spec/struct, struct-constructor, Struct Constructors)
1778
1804
* $(RELATIVE_LINK2 uniform_construction_syntax, Uniform construction syntax for built-in scalar types)
1779
1805
1780
1806
$(H3 $(LEGACY_LNAME2 index_operations, index_expressions, Index Operations))
0 commit comments