D Beginner Trying Manual Memory Management

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jan 12 12:30:35 PST 2015


On Mon, 12 Jan 2015 20:14:19 +0000
jmh530 via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com>
wrote:

> Thanks for the reply, I wasn't familiar with scoped. I was aware 
> that structs are on the stack and classes are on the heap in D, 
> but I didn't know it was possible to put a class on the stack. 
> Might be interesting to see how this is implemented.
actually, there is nothing complicated there (if you don't want to
write an universal thing like `emplace!` ;-). it builds a wrapper
struct big enough to hold class instance, copies class `.init` there
and calls class' constructor. the rest of the magic is done by the
compiler: when struct goes out of scope, compiler calls struct
destructor, which in turn calls class destructor. ah, and it forwards
all other requests with `alias this` trick.

> After looking up some more C++, I think what I was trying to do 
> is more like make_unique than unique_ptr.
i don't remember C++ well, but nevertheless i encouraging you to take a
look at `std.typecons`. there are some handy things there, like
`Rebindable!` or `Nullable!`. and some funny things like `BlackHole!`
and `WhiteHole!`. ;-)

it even has `RefCounted!`, but it doesn't play well with classes yet
(AFAIR).
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150112/f40fc027/attachment.sig>


More information about the Digitalmars-d-learn mailing list