rvalue references
Diggory
diggsey at googlemail.com
Tue Apr 23 12:24:43 PDT 2013
Based on DIP25/35 but slightly simplified, these rules would make
"ref" safe, would minimise code breakage, and only marginally
restricts the usage of "ref". The notion of "lifetime" is purely
a tool for simplifying the explanation of the rules.
Lifetimes
0 = temporary/rvalue
1 = local variable/non-ref parameter/scope-ref parameter
2 = ref parameter
3 = static/heap allocated value
A value with any lifetime may be passed as any parameter into a
function.
The & operator is safe only for lifetime >= 3, in other cases it
is unsafe or system
Only values with lifetime >= 2 can be returned by ref
The lifetime of a return value is determined by the caller as
follows:
- Take the lifetimes of all values passed in using "ref"
without the "scope" qualifier
- The lifetime of the return value is the minimum lifetime of
these values
OPTIONAL:
- A return value marked as "out" is guaranteed to have a lifetime
>= 3 (equivalent to all ref parameters being marked "scope")
- Marking a method as "scope" is equivalent to marking the "this"
parameter as such
More information about the Digitalmars-d
mailing list