@@ -645,16 +645,26 @@ $(H2 $(LNAME2 ref-functions, Ref Functions))
645
645
(whereas an expression formed by calling a non-`ref` function is an rvalue).
646
646
)
647
647
648
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
648
649
---
650
+ int *p;
651
+
649
652
ref int foo()
650
653
{
651
- auto p = new int(2);
654
+ p = new int(2);
652
655
return *p;
653
656
}
654
- ...
655
- int i = foo(); // i is set to 2
656
- foo() = 3; // reference returns can be lvalues
657
+
658
+ void main()
659
+ {
660
+ int i = foo();
661
+ assert(i == 2);
662
+
663
+ foo() = 3; // reference returns can be lvalues
664
+ assert(*p == 3);
665
+ }
657
666
---
667
+ )
658
668
659
669
$(P Returning a reference to an expired function context is not allowed.
660
670
This includes local variables, temporaries and parameters that are part
@@ -669,7 +679,8 @@ ref int sun()
669
679
}
670
680
---
671
681
672
- $(P A `ref` parameter may not be returned by `ref`.)
682
+ $(P A `ref` parameter may not be returned by `ref`, unless it is
683
+ $(RELATIVE_LINK2 return-ref-parameters, `return ref`).)
673
684
---
674
685
ref int moon(ref int i)
675
686
{
@@ -685,8 +696,7 @@ $(H2 $(LNAME2 auto-functions, Auto Functions))
685
696
)
686
697
687
698
$(P An auto function is declared without a return type.
688
- If it does not already have a storage class, use the
689
- $(D_KEYWORD auto) storage class.
699
+ Auto functions can use any valid $(GLINK2 declaration, StorageClass), not just `auto`.
690
700
)
691
701
692
702
$(P If there are multiple $(I ReturnStatement)s, the types
@@ -697,25 +707,37 @@ $(H2 $(LNAME2 auto-functions, Auto Functions))
697
707
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
698
708
---
699
709
auto foo(int x) { return x + 3; } // inferred to be int
700
- auto bar(int x) { return x; return 2.5; } // inferred to be double
710
+ pure bar(int x) { return x; return 2.5; } // inferred to be double
701
711
---
702
712
)
703
713
714
+ $(NOTE Return type inference also triggers
715
+ $(RELATIVE_LINK2 function-attribute-inference, attribute inference).)
716
+
717
+
704
718
$(H2 $(LNAME2 auto-ref-functions, Auto Ref Functions))
705
719
706
- $(P Auto ref functions infer their return type just as
720
+ $(P Auto ref functions can infer their return type just as
707
721
$(RELATIVE_LINK2 auto-functions, auto functions) do.
708
722
In addition, they become $(RELATIVE_LINK2 ref-functions, ref functions)
709
- if all return expressions are lvalues,
710
- and it would not be a reference to a local or a parameter.)
723
+ if all of these apply:)
724
+
725
+ * All expressions returned from the function are lvalues
726
+ * No local variables are returned
727
+ * Any parameters returned are reference parameters
728
+ * Each returned expression must implicitly convert to an lvalue of the deduced return type
711
729
712
730
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
713
731
---
714
732
auto ref f1(int x) { return x; } // value return
715
733
auto ref f2() { return 3; } // value return
716
734
auto ref f3(ref int x) { return x; } // ref return
717
735
auto ref f4(out int x) { return x; } // ref return
718
- auto ref f5() { static int x; return x; } // ref return
736
+ auto ref f5()
737
+ {
738
+ static int x;
739
+ return x; // ref return
740
+ }
719
741
---
720
742
)
721
743
@@ -729,16 +751,16 @@ $(H2 $(LNAME2 auto-ref-functions, Auto Ref Functions))
729
751
auto ref f3(ref int x, ref double y)
730
752
{
731
753
return x; return y;
732
- // The return type is deduced to double, but cast(double)x is not an lvalue,
733
- // then become a value return.
754
+ // The return type is deduced to be double, but cast(double)x is not an lvalue,
755
+ // so f3 has a value return.
734
756
}
735
757
---
736
758
)
737
759
738
- $(P Auto ref function can have explicit return type.)
760
+ $(P Auto ref functions can have an explicit return type.)
739
761
740
762
---
741
- auto ref int (ref int x) { return x; } // ok, ref return
763
+ auto ref int bar (ref int x) { return x; } // ok, ref return
742
764
auto ref int foo(double x) { return x; } // error, cannot convert double to int
743
765
---
744
766
0 commit comments