Skip to content

Commit ae4345e

Browse files
ntrelBolpat
authored andcommitted
Fix Bugzilla Issue 24868 - Undocumented cast from struct to static array
1 parent 4cc649d commit ae4345e

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
@@ -1492,6 +1492,24 @@ $(H4 $(LNAME2 cast_struct, Structs))
14921492
S(e)
14931493
---
14941494

1495+
$(P A struct instance can be cast to a static array type when
1496+
their `.sizeof` properties each give the same result.)
1497+
1498+
$(SPEC_RUNNABLE_EXAMPLE_RUN
1499+
---
1500+
struct S { short a, b, c; }
1501+
1502+
S s = S(1, 2, 3);
1503+
static assert(!__traits(compiles, cast(short[2]) s));
1504+
1505+
short[3] x = cast(short[3]) s;
1506+
assert(x.tupleof == s.tupleof);
1507+
1508+
auto y = cast(byte[6]) s;
1509+
assert(y == [1, 0, 2, 0, 3, 0]);
1510+
---
1511+
)
1512+
14951513
$(H4 $(LNAME2 cast_qualifier, Qualifier Cast))
14961514

14971515
$(GRAMMAR

0 commit comments

Comments
 (0)