Discussion: Rvalue refs and a Move construtor for D
Exil
Exil at gmall.com
Thu Sep 5 18:46:59 UTC 2019
On Thursday, 5 September 2019 at 15:49:37 UTC, kinke wrote:
> 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
There's still similar to the problem we have now. You're still
doing a memcpy() each time with S.init. And you are calling the
destructor now too, which entirely depends on what it is.
void foo(S value, int n = 0) {
if ( n > 32 ) {
return;
}
foo( move(value), n + 1);
}
S lvalue;
foo( move(lvalue) ); // "lvalue" destructed and set to S.init 32
times
More information about the Digitalmars-d
mailing list