[Issue 15662] New: Cannot move struct with defined opAssign due to @disabled post-blit

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Mon Feb 8 16:24:43 PST 2016


https://issues.dlang.org/show_bug.cgi?id=15662

          Issue ID: 15662
           Summary: Cannot move struct with defined opAssign due to
                    @disabled post-blit
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Windows
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: matt.elkins at gmail.com

Given the following definition:

[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]

The following will generate a compile error, making it awkward to move unique
resources:

[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]

See discussion at
https://forum.dlang.org/post/nnjfuqeuprcswsjjfmkl@forum.dlang.org

At first I was uncertain whether this was a bug or by design, but Andrei
Alexandrescu confirmed the bug status in the linked discussion.

--


More information about the Digitalmars-d-bugs mailing list