Skip to content

Commit a999c14

Browse files
ntreldlang-bot
authored andcommitted
[spec] Document ref variables
1 parent 12edffb commit a999c14

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

spec/declaration.dd

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,43 @@ void main()
852852
}
853853
--------
854854
)
855+
856+
$(H4 $(LNAME2 ref-variables, `ref` Variables))
857+
858+
$(P From version 2.111, `ref` can be used to declare local, static, extern, and global variables.)
859+
860+
$(COMMENT SPEC_RUNNABLE_EXAMPLE_RUN)
861+
---
862+
struct S { int a; }
863+
864+
void main()
865+
{
866+
S s;
867+
ref int r = s.a;
868+
r = 3;
869+
assert(s.a == 3);
870+
}
871+
---
872+
873+
$(BEST_PRACTICE Use a `ref` variable instead of a pointer when pointer
874+
arithmetic is not needed.)
875+
876+
$(P `auto ref` can also be used to declare local, static, extern, and global variables.)
877+
878+
$(COMMENT SPEC_RUNNABLE_EXAMPLE_COMPILE)
879+
---
880+
void f()
881+
{
882+
auto ref x = 0;
883+
auto ref y = x;
884+
static assert(!__traits(isRef, x));
885+
static assert( __traits(isRef, y));
886+
}
887+
---
888+
889+
$(P See also: $(DDSUBLINK spec/template, auto-ref-parameters, `auto ref`)
890+
template function parameters.)
891+
855892
$(H3 $(LNAME2 methods-returning-qualified, Methods Returning a Qualified Type))
856893

857894
$(P Some keywords, such as $(D const), can be used

0 commit comments

Comments
 (0)