@@ -486,27 +486,52 @@ constructor will have their `.init` values upon destruction.)
486
486
}
487
487
------
488
488
489
+ $(H2 $(LNAME2 disable_default_construction, Disabling Default Struct Construction))
490
+
489
491
$(P If struct constructor is annotated with $(D @disable) and has
490
- empty parameter, the struct is disabled construction without calling
491
- other constructor.
492
+ an empty $(GLINK2 function, ParameterList), the struct has disabled default construction.
493
+ The only way it can be constructed is via a call to another constructor with a non-empty
494
+ $(I ParameterList).
492
495
)
493
- ------
496
+
497
+ $(P A struct with a disabled default constructor, and no other constructors, cannot
498
+ be instantiated other than via a $(GLINK2 declaration, VoidInitializer).)
499
+
500
+ $(P A disabled default constructor may not have a $(GLINK2 function, FunctionBody).)
501
+
502
+ $(P If any fields have disabled default construction, the struct default construction is
503
+ also disabled.)
504
+
505
+ ---
494
506
struct S
495
507
{
496
508
int x;
497
509
498
- // Disables default construction, function body can be empty.
510
+ // Disables default construction
499
511
@disable this();
500
512
501
513
this(int v) { x = v; }
502
514
}
515
+ struct T
516
+ {
517
+ int y;
518
+ S s;
519
+ }
503
520
void main()
504
521
{
505
- //S s; // default construction is disabled
506
- //S s = S(); // also disabled
507
- S s = S(1); // construction with calling constructor
522
+ S s; // error: default construction is disabled
523
+ S t = S(); // error: also disabled
524
+ S u = S(1); // constructed by calling `S.this(1)`
525
+ S v = void; // not initialized, but allowed
526
+ S w = { 1 }; // error: cannot use { } since constructor exists
527
+ S[3] a; // error: default construction is disabled
528
+ S[3] b = [S(1), S(20), S(-2)]; // ok
529
+ T t; // error: default construction is disabled
508
530
}
509
- ------
531
+ ---
532
+
533
+ $(BEST_PRACTICE Disabling default construction is useful when the default value,
534
+ such as `null`, is not acceptable.)
510
535
511
536
$(H2 $(LEGACY_LNAME2 StructPostblit, struct-postblit, Struct Postblits))
512
537
0 commit comments