Declaring rvalue function arguments

Matt Elkins via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jan 31 10:02:19 PST 2016


On Sunday, 31 January 2016 at 17:48:53 UTC, maik klein wrote:
> The problem is that x will be copied afaik which is not what 
> you want if you want to deal with ownership.

I think that can be solved by wrapping the resource in a struct 
that deals with passing the ownership. Here is the one I am using 
right now:

[code]
struct ResourceHandle(T, alias Deleter, T Default = T.init)
{
     // Constructors/Destructor
     this(in 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 T handle() const {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]


More information about the Digitalmars-d-learn mailing list