@@ -1055,10 +1055,11 @@ $(H3 $(LNAME2 field-init, Field initialization inside a constructor))
1055
1055
---
1056
1056
)
1057
1057
1058
- $(H3 $(LEGACY_LNAME2 StructCopyConstructor, struct-copy-constructor, Struct Copy Constructors))
1058
+ $(H2 $(LEGACY_LNAME2 StructCopyConstructor, struct-copy-constructor, Struct Copy Constructors))
1059
1059
1060
1060
$(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).)
1062
1063
1063
1064
$(P A constructor declaration is a copy constructor declaration if it meets
1064
1065
the following requirements:)
@@ -1094,20 +1095,31 @@ $(H3 $(LEGACY_LNAME2 StructCopyConstructor, struct-copy-constructor, Struct Copy
1094
1095
1095
1096
$(OL
1096
1097
$(LI When a variable is explicitly initialized:)
1098
+
1099
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1097
1100
---
1098
1101
struct A
1099
1102
{
1100
- this(ref return scope A rhs) {}
1103
+ int[] arr;
1104
+ this(ref return scope A rhs) { arr = rhs.arr.dup; }
1101
1105
}
1102
1106
1103
1107
void main()
1104
1108
{
1105
1109
A a;
1110
+ a.arr = [1, 2];
1111
+
1106
1112
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]);
1107
1116
}
1108
1117
---
1118
+ )
1109
1119
1110
1120
$(LI When a parameter is passed by value to a function:)
1121
+
1122
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
1111
1123
---
1112
1124
struct A
1113
1125
{
@@ -1122,9 +1134,12 @@ $(H3 $(LEGACY_LNAME2 StructCopyConstructor, struct-copy-constructor, Struct Copy
1122
1134
fun(a); // copy constructor gets called
1123
1135
}
1124
1136
---
1137
+ )
1125
1138
1126
- $(LI When a parameter is returned by value from a function and Named Returned Value Optiomization (NRVO)
1139
+ $(LI When a parameter is returned by value from a function and Named Returned Value Optimization (NRVO)
1127
1140
cannot be performed:)
1141
+
1142
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
1128
1143
---
1129
1144
struct A
1130
1145
{
@@ -1150,11 +1165,15 @@ $(H3 $(LEGACY_LNAME2 StructCopyConstructor, struct-copy-constructor, Struct Copy
1150
1165
}
1151
1166
---
1152
1167
)
1168
+ )
1169
+
1170
+ $(H3 $(LNAME2 disable-copy, Disabled Copying))
1153
1171
1154
- $(LNAME2 disable-copy)
1155
1172
$(P When a copy constructor is defined for a `struct` (or marked `@disable`), the compiler no
1156
1173
longer implicitly generates default copy/blitting constructors for that `struct`:
1157
1174
)
1175
+
1176
+ $(SPEC_RUNNABLE_EXAMPLE_FAIL
1158
1177
---
1159
1178
struct A
1160
1179
{
@@ -1170,7 +1189,9 @@ $(H3 $(LEGACY_LNAME2 StructCopyConstructor, struct-copy-constructor, Struct Copy
1170
1189
fun(a); // error: copy constructor cannot be called with types (immutable) immutable
1171
1190
}
1172
1191
---
1192
+ )
1173
1193
1194
+ $(SPEC_RUNNABLE_EXAMPLE_FAIL
1174
1195
---
1175
1196
struct A
1176
1197
{
@@ -1183,19 +1204,40 @@ $(H3 $(LEGACY_LNAME2 StructCopyConstructor, struct-copy-constructor, Struct Copy
1183
1204
A b = a; // error: copy constructor is disabled
1184
1205
}
1185
1206
---
1207
+ )
1186
1208
1187
- $(P If a `union S ` has fields that define a copy constructor, whenever an object of type `S `
1209
+ $(P If a `union U ` has fields that define a copy constructor, whenever an object of type `U `
1188
1210
is initialized by copy, an error will be issued. The same rule applies to overlapped fields
1189
1211
(anonymous unions).)
1190
1212
1191
- $(P A `struct` that defines a copy constructor is not $(RELATIVE_LINK2 POD, POD).)
1213
+ $(SPEC_RUNNABLE_EXAMPLE_FAIL
1214
+ ---
1215
+ struct S
1216
+ {
1217
+ this(ref S);
1218
+ }
1219
+
1220
+ union U
1221
+ {
1222
+ S s;
1223
+ }
1224
+
1225
+ void main()
1226
+ {
1227
+ U a;
1228
+ U b = a; // error, could not generate copy constructor for U
1229
+ }
1230
+ ---
1231
+ )
1192
1232
1193
- $(H4 $(LNAME2 copy-constructor-attributes, Copy Constructor Attributes))
1233
+ $(H3 $(LNAME2 copy-constructor-attributes, Copy Constructor Attributes))
1194
1234
1195
1235
$(P The copy constructor can be overloaded with different qualifiers applied
1196
1236
to the parameter (copying from a qualified source) or to the copy constructor
1197
1237
itself (copying to a qualified destination):
1198
1238
)
1239
+
1240
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
1199
1241
---
1200
1242
struct A
1201
1243
{
@@ -1216,10 +1258,13 @@ $(H4 $(LNAME2 copy-constructor-attributes, Copy Constructor Attributes))
1216
1258
immutable A a5 = ia; // calls 4
1217
1259
}
1218
1260
---
1261
+ )
1219
1262
1220
1263
$(P The `inout` qualifier may be applied to the copy constructor parameter in
1221
1264
order to specify that mutable, `const`, or `immutable` types are treated the same:
1222
1265
)
1266
+
1267
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
1223
1268
---
1224
1269
struct A
1225
1270
{
@@ -1238,8 +1283,9 @@ $(H4 $(LNAME2 copy-constructor-attributes, Copy Constructor Attributes))
1238
1283
immutable(A) c = r3;
1239
1284
}
1240
1285
---
1286
+ )
1241
1287
1242
- $(H4 $(LNAME2 implicit-copy-constructors, Implicit Copy Constructors))
1288
+ $(H3 $(LNAME2 implicit-copy-constructors, Implicit Copy Constructors))
1243
1289
1244
1290
$(P A copy constructor is generated implicitly by the compiler for a `struct S`
1245
1291
if all of the following conditions are met:)
@@ -1251,6 +1297,7 @@ $(H4 $(LNAME2 implicit-copy-constructors, Implicit Copy Constructors))
1251
1297
)
1252
1298
1253
1299
$(P If the restrictions above are met, the following copy constructor is generated:)
1300
+
1254
1301
---
1255
1302
this(ref return scope inout(S) src) inout
1256
1303
{
@@ -1270,7 +1317,7 @@ $(GNAME Postblit):
1270
1317
$(D this $(LPAREN) this $(RPAREN)) $(GLINK2 function, MemberFunctionAttributes)$(OPT) $(GLINK2 function, FunctionBody)
1271
1318
)
1272
1319
1273
- $(P WARNING : The postblit is considered legacy and is not recommended for new code.
1320
+ $(P $(RED Warning) : The postblit is considered legacy and is not recommended for new code.
1274
1321
Code should use $(RELATIVE_LINK2 struct-copy-constructor, copy constructors)
1275
1322
defined in the previous section. For backward compatibility reasons, a `struct` that
1276
1323
explicitly defines both a copy constructor and a postblit will only use the postblit
@@ -1280,7 +1327,7 @@ $(GNAME Postblit):
1280
1327
will have priority over the copy constructor.)
1281
1328
1282
1329
$(P $(I Copy construction) is defined as initializing
1283
- a struct instance from another struct of the same type.
1330
+ a struct instance from another instance of the same type.
1284
1331
Copy construction is divided into two parts:)
1285
1332
1286
1333
$(OL
@@ -1298,7 +1345,7 @@ $(GNAME Postblit):
1298
1345
etc. For example:
1299
1346
)
1300
1347
1301
- $(SPEC_RUNNABLE_EXAMPLE_RUN
1348
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
1302
1349
---
1303
1350
struct S
1304
1351
{
0 commit comments