Skip to content

Commit 09e06b0

Browse files
committed
Show complete example of copy ctor
1 parent 01b1c96 commit 09e06b0

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

spec/struct.dd

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,17 +1096,23 @@ $(H2 $(LEGACY_LNAME2 StructCopyConstructor, struct-copy-constructor, Struct Copy
10961096
$(OL
10971097
$(LI When a variable is explicitly initialized:)
10981098

1099-
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
1099+
$(SPEC_RUNNABLE_EXAMPLE_RUN
11001100
---
11011101
struct A
11021102
{
1103-
this(ref return scope A rhs) {}
1103+
int[] arr;
1104+
this(ref return scope A rhs) { arr = rhs.arr.dup; }
11041105
}
11051106

11061107
void main()
11071108
{
11081109
A a;
1110+
a.arr = [1, 2];
1111+
11091112
A b = a; // copy constructor gets called
1113+
b.arr[] += 1;
1114+
assert(a.arr == [1, 2]); // a is unchanged
1115+
assert(b.arr == [2, 3]);
11101116
}
11111117
---
11121118
)

0 commit comments

Comments
 (0)