Skip to content

Commit 01b1c96

Browse files
committed
[spec/struct] Tweak Copy Constructor docs
Change headings to be independent of Struct Constructor section, because copy ctor section is quite long with subheadings that should be shown in the TOC. Move sentence up about POD. Make examples runnable. Add Disabled Copying subheading. Minor tweaks. Add example for union with field of type struct which has a copy ctor.
1 parent 2a069fa commit 01b1c96

File tree

1 file changed

+52
-11
lines changed

1 file changed

+52
-11
lines changed

spec/struct.dd

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,10 +1055,11 @@ $(H3 $(LNAME2 field-init, Field initialization inside a constructor))
10551055
---
10561056
)
10571057

1058-
$(H3 $(LEGACY_LNAME2 StructCopyConstructor, struct-copy-constructor, Struct Copy Constructors))
1058+
$(H2 $(LEGACY_LNAME2 StructCopyConstructor, struct-copy-constructor, Struct Copy Constructors))
10591059

10601060
$(P Copy constructors are used to initialize a `struct` instance from
1061-
another `struct` of the same type.)
1061+
another instance of the same type. A `struct` that defines a copy constructor
1062+
is not $(RELATIVE_LINK2 POD, POD).)
10621063

10631064
$(P A constructor declaration is a copy constructor declaration if it meets
10641065
the following requirements:)
@@ -1094,6 +1095,8 @@ $(H3 $(LEGACY_LNAME2 StructCopyConstructor, struct-copy-constructor, Struct Copy
10941095

10951096
$(OL
10961097
$(LI When a variable is explicitly initialized:)
1098+
1099+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
10971100
---
10981101
struct A
10991102
{
@@ -1106,8 +1109,11 @@ $(H3 $(LEGACY_LNAME2 StructCopyConstructor, struct-copy-constructor, Struct Copy
11061109
A b = a; // copy constructor gets called
11071110
}
11081111
---
1112+
)
11091113

11101114
$(LI When a parameter is passed by value to a function:)
1115+
1116+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
11111117
---
11121118
struct A
11131119
{
@@ -1122,9 +1128,12 @@ $(H3 $(LEGACY_LNAME2 StructCopyConstructor, struct-copy-constructor, Struct Copy
11221128
fun(a); // copy constructor gets called
11231129
}
11241130
---
1131+
)
11251132

1126-
$(LI When a parameter is returned by value from a function and Named Returned Value Optiomization (NRVO)
1133+
$(LI When a parameter is returned by value from a function and Named Returned Value Optimization (NRVO)
11271134
cannot be performed:)
1135+
1136+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
11281137
---
11291138
struct A
11301139
{
@@ -1150,11 +1159,15 @@ $(H3 $(LEGACY_LNAME2 StructCopyConstructor, struct-copy-constructor, Struct Copy
11501159
}
11511160
---
11521161
)
1162+
)
1163+
1164+
$(H3 $(LNAME2 disable-copy, Disabled Copying))
11531165

1154-
$(LNAME2 disable-copy)
11551166
$(P When a copy constructor is defined for a `struct` (or marked `@disable`), the compiler no
11561167
longer implicitly generates default copy/blitting constructors for that `struct`:
11571168
)
1169+
1170+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
11581171
---
11591172
struct A
11601173
{
@@ -1170,7 +1183,9 @@ $(H3 $(LEGACY_LNAME2 StructCopyConstructor, struct-copy-constructor, Struct Copy
11701183
fun(a); // error: copy constructor cannot be called with types (immutable) immutable
11711184
}
11721185
---
1186+
)
11731187

1188+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
11741189
---
11751190
struct A
11761191
{
@@ -1183,19 +1198,40 @@ $(H3 $(LEGACY_LNAME2 StructCopyConstructor, struct-copy-constructor, Struct Copy
11831198
A b = a; // error: copy constructor is disabled
11841199
}
11851200
---
1201+
)
11861202

1187-
$(P If a `union S` has fields that define a copy constructor, whenever an object of type `S`
1203+
$(P If a `union U` has fields that define a copy constructor, whenever an object of type `U`
11881204
is initialized by copy, an error will be issued. The same rule applies to overlapped fields
11891205
(anonymous unions).)
11901206

1191-
$(P A `struct` that defines a copy constructor is not $(RELATIVE_LINK2 POD, POD).)
1207+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
1208+
---
1209+
struct S
1210+
{
1211+
this(ref S);
1212+
}
11921213

1193-
$(H4 $(LNAME2 copy-constructor-attributes, Copy Constructor Attributes))
1214+
union U
1215+
{
1216+
S s;
1217+
}
1218+
1219+
void main()
1220+
{
1221+
U a;
1222+
U b = a; // error, could not generate copy constructor for U
1223+
}
1224+
---
1225+
)
1226+
1227+
$(H3 $(LNAME2 copy-constructor-attributes, Copy Constructor Attributes))
11941228

11951229
$(P The copy constructor can be overloaded with different qualifiers applied
11961230
to the parameter (copying from a qualified source) or to the copy constructor
11971231
itself (copying to a qualified destination):
11981232
)
1233+
1234+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
11991235
---
12001236
struct A
12011237
{
@@ -1216,10 +1252,13 @@ $(H4 $(LNAME2 copy-constructor-attributes, Copy Constructor Attributes))
12161252
immutable A a5 = ia; // calls 4
12171253
}
12181254
---
1255+
)
12191256

12201257
$(P The `inout` qualifier may be applied to the copy constructor parameter in
12211258
order to specify that mutable, `const`, or `immutable` types are treated the same:
12221259
)
1260+
1261+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
12231262
---
12241263
struct A
12251264
{
@@ -1238,8 +1277,9 @@ $(H4 $(LNAME2 copy-constructor-attributes, Copy Constructor Attributes))
12381277
immutable(A) c = r3;
12391278
}
12401279
---
1280+
)
12411281

1242-
$(H4 $(LNAME2 implicit-copy-constructors, Implicit Copy Constructors))
1282+
$(H3 $(LNAME2 implicit-copy-constructors, Implicit Copy Constructors))
12431283

12441284
$(P A copy constructor is generated implicitly by the compiler for a `struct S`
12451285
if all of the following conditions are met:)
@@ -1251,6 +1291,7 @@ $(H4 $(LNAME2 implicit-copy-constructors, Implicit Copy Constructors))
12511291
)
12521292

12531293
$(P If the restrictions above are met, the following copy constructor is generated:)
1294+
12541295
---
12551296
this(ref return scope inout(S) src) inout
12561297
{
@@ -1270,7 +1311,7 @@ $(GNAME Postblit):
12701311
$(D this $(LPAREN) this $(RPAREN)) $(GLINK2 function, MemberFunctionAttributes)$(OPT) $(GLINK2 function, FunctionBody)
12711312
)
12721313

1273-
$(P WARNING: The postblit is considered legacy and is not recommended for new code.
1314+
$(P $(RED Warning): The postblit is considered legacy and is not recommended for new code.
12741315
Code should use $(RELATIVE_LINK2 struct-copy-constructor, copy constructors)
12751316
defined in the previous section. For backward compatibility reasons, a `struct` that
12761317
explicitly defines both a copy constructor and a postblit will only use the postblit
@@ -1280,7 +1321,7 @@ $(GNAME Postblit):
12801321
will have priority over the copy constructor.)
12811322

12821323
$(P $(I Copy construction) is defined as initializing
1283-
a struct instance from another struct of the same type.
1324+
a struct instance from another instance of the same type.
12841325
Copy construction is divided into two parts:)
12851326

12861327
$(OL
@@ -1298,7 +1339,7 @@ $(GNAME Postblit):
12981339
etc. For example:
12991340
)
13001341

1301-
$(SPEC_RUNNABLE_EXAMPLE_RUN
1342+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
13021343
---
13031344
struct S
13041345
{

0 commit comments

Comments
 (0)