D's equivalent to C++'s std::move?

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Wed Feb 3 18:33:06 PST 2016


On 02/03/2016 09:01 PM, Matt Elkins wrote:
>
> [code]
> import std.algorithm;
>
> struct ResourceHandle(T, alias Deleter, T Default = T.init)
> {
>      // Constructors/Destructor
>      this(T handle) {m_handle = handle;}
>      @disable this(this);
>      ~this() {Deleter(m_handle);}
>
>      // Operators
>      @disable void opAssign(ref ResourceHandle lvalue);
>      ref ResourceHandle opAssign(ResourceHandle rvalue) {swap(m_handle,
> rvalue.m_handle); return this;}
>
>      // Methods
>      @property inout(T) handle() inout {return m_handle;}
>      @property T handle(T handle) {Deleter(m_handle); m_handle = handle;
> return m_handle;}
>      T release() {T result = m_handle; m_handle = Default; return result;}
>
>      private:
>          T m_handle = Default;
> }
> [/code]
>
> This seems to cover most of my bases, but I still can't do things like
> this:
>
> [code]
> unittest
> {
>      alias RH = ResourceHandle!(uint, (uint) {});
>      RH[] handles;
>      handles ~= RH(5); // Compile error: ResourceHandle is not copyable
> because it is annotated with @disable
> }
> [/code]

Got it, thanks. That's a bug in the implementation, no two ways about 
it. No copy should occur there, neither theoretically nor practically. 
Please report it to bugzilla at http://issues.dlang.org. Thanks very 
much! -- Andrei



More information about the Digitalmars-d mailing list