Skip to content

Commit 560132e

Browse files
ntreldlang-bot
authored andcommitted
Fix Bugzilla Issue 24868 - Undocumented cast from struct to static array
1 parent 3f045b7 commit 560132e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

spec/expression.dd

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,24 @@ $(H4 $(LNAME2 cast_struct, Structs))
15351535
S(e)
15361536
---
15371537

1538+
$(P A struct instance can be cast to a static array type when
1539+
their `.sizeof` properties each give the same result.)
1540+
1541+
$(SPEC_RUNNABLE_EXAMPLE_RUN
1542+
---
1543+
struct S { short a, b, c; }
1544+
1545+
S s = S(1, 2, 3);
1546+
static assert(!__traits(compiles, cast(short[2]) s));
1547+
1548+
short[3] x = cast(short[3]) s;
1549+
assert(x.tupleof == s.tupleof);
1550+
1551+
auto y = cast(byte[6]) s;
1552+
assert(y == [1, 0, 2, 0, 3, 0]);
1553+
---
1554+
)
1555+
15381556
$(H4 $(LNAME2 cast_qualifier, Qualifier Cast))
15391557

15401558
$(GRAMMAR

0 commit comments

Comments
 (0)