Discussion: Rvalue refs and a Move construtor for D

kinke kinke at gmx.net
Thu Sep 5 15:49:37 UTC 2019


On Thursday, 5 September 2019 at 15:32:31 UTC, Suleyman wrote:
> Practical example:
> [...]
> void main()
> {
>     auto s = S(_1GB);
>     bar(__move(s));  // calls move ctor
>     assert(s.length == 0); // s is still usable
> }

Yes, that's what C++ does, and is not as efficient as can be. 
What I'm looking for is that there's no actual moving (move ctor 
call etc.) at all for move/__move in an argument expression. In 
your code example:

void main()
{
     auto s = S(_1GB);
     // does NOT call move ctor, just passes `s` by ref directly 
instead of a moved-to
     // temporary
     bar(__move(s));
     // after the call, destruct `s` and reset to T.init
     assert(s.length == 0); // s is still usable
     // this now does call the move ctor:
     auto s2 = __move(s);
} // `s` goes out of scope and is destructed again


More information about the Digitalmars-d mailing list