Discussion: Rvalue refs and a Move construtor for D
Suleyman
sahmi.soulaimane at gmail.com
Thu Sep 5 01:30:02 UTC 2019
On Wednesday, 4 September 2019 at 17:41:02 UTC, Suleyman wrote:
> [...]
I expanded rvalue ref in the POC. It is now possible to have a
implementation of `forward` based on rvalue ref.
Example:
```
template forward(alias arg)
{
// rvalue ref
enum isRvalue = __traits(isRvalueRef, arg) ||
!(__traits(isRef, arg) ||
__traits(isOut, arg) ||
__traits(isLazy, arg));
static if (isRvalue)
@property @rvalue ref forward() { return __move(arg); }
else
alias forward = arg;
}
unittest
{
class C
{
import core.stdc.stdio : puts;
static void foo(int n) { puts("foo value"); }
static void foo(ref int n) { puts("foo ref"); }
static void bar(@rvalue ref int n) { puts("bar rvalue
ref"); }
static void bar(ref int n) { puts("bar ref"); }
}
void foo()(auto ref int x) { C.foo(forward!x); }
void bar()(auto ref int x) { C.bar(forward!x); }
int i;
foo(1);
foo(i);
bar(1);
bar(i);
}
```
I don't see how we can achieve the same efficiency without rvalue
ref.
More information about the Digitalmars-d
mailing list