@@ -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,6 +1095,8 @@ $(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_COMPILE
1097
1100
---
1098
1101
struct A
1099
1102
{
@@ -1106,8 +1109,11 @@ $(H3 $(LEGACY_LNAME2 StructCopyConstructor, struct-copy-constructor, Struct Copy
1106
1109
A b = a; // copy constructor gets called
1107
1110
}
1108
1111
---
1112
+ )
1109
1113
1110
1114
$(LI When a parameter is passed by value to a function:)
1115
+
1116
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
1111
1117
---
1112
1118
struct A
1113
1119
{
@@ -1122,9 +1128,12 @@ $(H3 $(LEGACY_LNAME2 StructCopyConstructor, struct-copy-constructor, Struct Copy
1122
1128
fun(a); // copy constructor gets called
1123
1129
}
1124
1130
---
1131
+ )
1125
1132
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)
1127
1134
cannot be performed:)
1135
+
1136
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
1128
1137
---
1129
1138
struct A
1130
1139
{
@@ -1150,11 +1159,15 @@ $(H3 $(LEGACY_LNAME2 StructCopyConstructor, struct-copy-constructor, Struct Copy
1150
1159
}
1151
1160
---
1152
1161
)
1162
+ )
1163
+
1164
+ $(H3 $(LNAME2 disable-copy, Disabled Copying))
1153
1165
1154
- $(LNAME2 disable-copy)
1155
1166
$(P When a copy constructor is defined for a `struct` (or marked `@disable`), the compiler no
1156
1167
longer implicitly generates default copy/blitting constructors for that `struct`:
1157
1168
)
1169
+
1170
+ $(SPEC_RUNNABLE_EXAMPLE_FAIL
1158
1171
---
1159
1172
struct A
1160
1173
{
@@ -1170,7 +1183,9 @@ $(H3 $(LEGACY_LNAME2 StructCopyConstructor, struct-copy-constructor, Struct Copy
1170
1183
fun(a); // error: copy constructor cannot be called with types (immutable) immutable
1171
1184
}
1172
1185
---
1186
+ )
1173
1187
1188
+ $(SPEC_RUNNABLE_EXAMPLE_FAIL
1174
1189
---
1175
1190
struct A
1176
1191
{
@@ -1183,19 +1198,40 @@ $(H3 $(LEGACY_LNAME2 StructCopyConstructor, struct-copy-constructor, Struct Copy
1183
1198
A b = a; // error: copy constructor is disabled
1184
1199
}
1185
1200
---
1201
+ )
1186
1202
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 `
1188
1204
is initialized by copy, an error will be issued. The same rule applies to overlapped fields
1189
1205
(anonymous unions).)
1190
1206
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
+ }
1192
1213
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))
1194
1228
1195
1229
$(P The copy constructor can be overloaded with different qualifiers applied
1196
1230
to the parameter (copying from a qualified source) or to the copy constructor
1197
1231
itself (copying to a qualified destination):
1198
1232
)
1233
+
1234
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
1199
1235
---
1200
1236
struct A
1201
1237
{
@@ -1216,10 +1252,13 @@ $(H4 $(LNAME2 copy-constructor-attributes, Copy Constructor Attributes))
1216
1252
immutable A a5 = ia; // calls 4
1217
1253
}
1218
1254
---
1255
+ )
1219
1256
1220
1257
$(P The `inout` qualifier may be applied to the copy constructor parameter in
1221
1258
order to specify that mutable, `const`, or `immutable` types are treated the same:
1222
1259
)
1260
+
1261
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
1223
1262
---
1224
1263
struct A
1225
1264
{
@@ -1238,8 +1277,9 @@ $(H4 $(LNAME2 copy-constructor-attributes, Copy Constructor Attributes))
1238
1277
immutable(A) c = r3;
1239
1278
}
1240
1279
---
1280
+ )
1241
1281
1242
- $(H4 $(LNAME2 implicit-copy-constructors, Implicit Copy Constructors))
1282
+ $(H3 $(LNAME2 implicit-copy-constructors, Implicit Copy Constructors))
1243
1283
1244
1284
$(P A copy constructor is generated implicitly by the compiler for a `struct S`
1245
1285
if all of the following conditions are met:)
@@ -1251,6 +1291,7 @@ $(H4 $(LNAME2 implicit-copy-constructors, Implicit Copy Constructors))
1251
1291
)
1252
1292
1253
1293
$(P If the restrictions above are met, the following copy constructor is generated:)
1294
+
1254
1295
---
1255
1296
this(ref return scope inout(S) src) inout
1256
1297
{
@@ -1270,7 +1311,7 @@ $(GNAME Postblit):
1270
1311
$(D this $(LPAREN) this $(RPAREN)) $(GLINK2 function, MemberFunctionAttributes)$(OPT) $(GLINK2 function, FunctionBody)
1271
1312
)
1272
1313
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.
1274
1315
Code should use $(RELATIVE_LINK2 struct-copy-constructor, copy constructors)
1275
1316
defined in the previous section. For backward compatibility reasons, a `struct` that
1276
1317
explicitly defines both a copy constructor and a postblit will only use the postblit
@@ -1280,7 +1321,7 @@ $(GNAME Postblit):
1280
1321
will have priority over the copy constructor.)
1281
1322
1282
1323
$(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.
1284
1325
Copy construction is divided into two parts:)
1285
1326
1286
1327
$(OL
@@ -1298,7 +1339,7 @@ $(GNAME Postblit):
1298
1339
etc. For example:
1299
1340
)
1300
1341
1301
- $(SPEC_RUNNABLE_EXAMPLE_RUN
1342
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
1302
1343
---
1303
1344
struct S
1304
1345
{
0 commit comments