D's equivalent to C++'s std::move?
Atila Neves via Digitalmars-d
digitalmars-d at puremagic.com
Thu Feb 11 08:31:03 PST 2016
On Thursday, 11 February 2016 at 14:38:24 UTC, Ola Fosheim
Grøstad wrote:
> On Thursday, 11 February 2016 at 14:25:39 UTC, Atila Neves
> wrote:
>> D has move semantics. Deep copies are done with post-blit.
>> Fair enough if you just:
>>
>> auto foo = bar;
>>
>> Then it's a shallow copy. The only difference to a "true" move
>> is that bar isn't T.init, but that's easily done with the move
>> function (assuming the struct has a destructor) or manually.
>
> *blank stare*
Err... ok.
>> C++:
>>
>> void foo(Foo); //copy
>> void foo(Foo&); //by-ref, only lvalues
>> void foo(Foo&&); //move, only rvalues
>
> In modern generics-oriented C++ I would say:
>
> void foo(T); // by value - and probably not what you want
It depends. For small structs, it is. And in some cases the
compiler can elide the copy.
> void foo(const T&) // copy semantics overload
> void foo(T&&) // move semantics overload
I forgot the const. It doesn't change my point.
> Please keep in mind that C++ do perfect forwarding of those
> rvalue references when you pass it down a call chain.
No it doesn't. It _allows_ you to perfect forward, as long as you
remember to use `std::forward`. And in that case, they're not
really rvalue references, they're forwarding references (what
Scott Meyers initially called universal references).
The only issue that I know of with D's approach is that, if you
want to pass by ref for efficiency reasons, then you can't pass
an rvalue in. It's never been a problem for me.
Atila
More information about the Digitalmars-d
mailing list