Discussion: Rvalue refs and a Move construtor for D

Suleyman sahmi.soulaimane at gmail.com
Wed Aug 28 23:32:01 UTC 2019


Hello everybody.


Recently D adpoted the copy constructor, the postblit is 
deprecated, and the postmove (DIP 1014) is also deprecated since 
it has the same problems as the postblit.

I am planning in this SOAC of 2019 to work on an alternative to 
the postmove (DIP 1014) which will be similar to C++' move 
constructor and move assignment operator. But in C++ these take 
an rvalue ref argument which we don't have in D (yet).

Move operations are already done by the compiler whether we have 
rvalue ref or not.

The move constructor and the move assignment operator can be made 
in D with or without rvalue refs. Here a short comparison of both 
scenarios.

## With rvalue ref

Example:
```
struct S
{
     this(@rvalue ref S) { /* move constructor */ }
     auto opAssign(@rvalue ref S) { /* move op assign */ }
}
```

* Pros:
  - seems natural: doesn't smell like compiler magic (but it still 
is compiler magic, at least until D adopts implicit constructors)
  - overloadable with other constructors and opAssign declarations

*Cons
  - requires adding rvalue ref to the language

## Without rvalue refs

Example:
```
struct S
{
     __move_ctor(ref S) { /* move constructor */ }
     auto opMoveAssign(ref S) { /* move op assign */ }
}
```

* Pros:
  - doesn't require extra features in the language

* Cons:
  - ugly constructor name: otherwise it would clash with the copy 
constructor.
  - doesn't overload with regular constructor or regular opAssign


Give me your thoughts on which way you prefer. And most 
importantly I want you to convince me why adding rvalue refs is 
good for D because there aren't many use cases that I know of.



More information about the Digitalmars-d mailing list