Skip to content

Commit 62c8761

Browse files
authored
Document enum copying and assignment behavior (#3716)
An enum derived from a struct type bypasses its base type's copy constructor, postblit, and identity opAssign overloads (if any). This behavior is a special case, and does not apply to other operator overloads or special member functions of the base type (e.g., destructors), so it should be documented explicitly.
1 parent da48679 commit 62c8761

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

spec/enum.dd

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,75 @@ X.sizeof // is same as int.sizeof
201201
in order to compute the $(CODE .max) and $(CODE .min) properties.
202202
)
203203

204+
$(H3 $(LNAME2 enum_copying_and_assignment, Enum Copying and Assignment))
205+
206+
$(P A named enum type never has a $(DDSUBLINK spec/struct,
207+
struct-copy-constructor, copy constructor), $(DDSUBLINK spec/struct,
208+
struct-postblit, postblit), or $(DDSUBLINK spec/struct,
209+
assign-overload, identity assignment overload), even if one is defined
210+
by its $(GLINK EnumBaseType).)
211+
212+
$(P When copying a named enum value whose base type is a `struct` with
213+
a copy constructor, the copy constructor is not called:)
214+
215+
$(SPEC_RUNNABLE_EXAMPLE_RUN
216+
---
217+
struct S
218+
{
219+
this(ref S rhs) { assert(0); }
220+
}
221+
222+
enum E : S { A = S.init }
223+
224+
void main()
225+
{
226+
E e1;
227+
E e2 = e1; // ok - copy constructor not called
228+
}
229+
---
230+
)
231+
232+
$(P When copying a named enum value whose base type is a `struct` with
233+
a postblit, the postblit is not called:)
234+
235+
$(SPEC_RUNNABLE_EXAMPLE_RUN
236+
---
237+
struct S
238+
{
239+
this(this) { assert(0); }
240+
}
241+
242+
enum E : S { A = S.init }
243+
244+
void main()
245+
{
246+
E e1;
247+
E e2 = e1; // ok - postblit not called
248+
}
249+
---
250+
)
251+
252+
$(P When assigning a named enum value to another object of the same
253+
type, if the base type of those values is a `struct` with an identity
254+
assignment overload, the identity assignment overload is not called:)
255+
256+
$(SPEC_RUNNABLE_EXAMPLE_RUN
257+
---
258+
struct S
259+
{
260+
void opAssign(S rhs) { assert(0); }
261+
}
262+
263+
enum E : S { A = S.init }
264+
265+
void main()
266+
{
267+
E e1, e2;
268+
e2 = e1; // ok - opAssign not called
269+
}
270+
---
271+
)
272+
204273

205274
$(H2 $(LNAME2 anonymous_enums, Anonymous Enums))
206275

0 commit comments

Comments
 (0)