File tree Expand file tree Collapse file tree 1 file changed +21
-5
lines changed Expand file tree Collapse file tree 1 file changed +21
-5
lines changed Original file line number Diff line number Diff line change @@ -604,18 +604,34 @@ $(GNAME VoidInitializer):
604
604
$(P Normally, variables are initialized either with an explicit
605
605
$(GLINK Initializer) or are set to the default value for the
606
606
type of the variable. If the $(I Initializer) is $(D void),
607
- however, the variable is not initialized. If its value is
608
- used before it is set, undefined program behavior will result.
607
+ however, the variable is not initialized.
609
608
)
610
609
611
- $(UNDEFINED_BEHAVIOR If a void initialized variable's value is
612
- used before it is set, the behavior is undefined .
610
+ $(IMPLEMENTATION_DEFINED If a void initialized variable's value is
611
+ used before it is set, the value is implementation defined .
613
612
614
613
---
614
+ import std.stdio;
615
615
void foo()
616
616
{
617
617
int x = void;
618
- writeln(x); // will print garbage
618
+ writeln(x); // will likely print garbage
619
+ }
620
+ ---
621
+ )
622
+
623
+ $(UNDEFINED_BEHAVIOR If a void initialized variable is a reference type
624
+ and its value is dereferenced before it is set, the behavior is undefined.
625
+ Such initializations are not allowed in `@safe` code.
626
+
627
+ ---
628
+ void foo()
629
+ {
630
+ int* p = void;
631
+ *p = 3; // undefined behavior
632
+ int x;
633
+ p = &x;
634
+ *p = 3; // ok
619
635
}
620
636
---
621
637
)
You can’t perform that action at this time.
0 commit comments