struct template constructors
Boris-Barboris via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Jun 22 12:30:24 PDT 2017
On Thursday, 22 June 2017 at 19:17:13 UTC, Ali Çehreli wrote:
> No time to think about the rest of the design but just to get
> the code compiled, replace 'ref' with 'auto ref' like so:
>
> this(DT)(scope auto ref UniquePtr!DT rhs)
> {
> // ...
> }
>
> Ali
i added this static variable:
static int destcalls = 0;
changed constructor to:
this(DT)(scope auto ref UniquePtr!DT rhs)
{
// here rhs releases pointer
pragma(msg, typeof(rhs));
}
wich prints "UniquePtr!(B)" on my machine,
and destructor:
~this() { destcalls++; }
Following code compiles and runs ok:
class A {}
class B: A {}
UniquePtr!A a = UniquePtr!A.make();
assert(destcalls == 0);
UniquePtr!A b = UniquePtr!B.make();
assert(destcalls == 1);
Destructor is called for the result of "UniquePtr!B.make();", and
if it actually was passed by value (wich is indicated by pragma's
output), b's internal pointer would refer to freed heap block.
More information about the Digitalmars-d-learn
mailing list