[Issue 14078] Call DTor for stack allocated objects

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Jan 29 14:15:32 PST 2015


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

--- Comment #2 from rswhite4 at gmail.com ---
(In reply to Ketmar Dark from comment #1)
> `emplace` is not a built-in. one can write alot of emplace-like functions
> that allocates any memory for the object. even the function named `emplace`,
> that will sometimes allocates on stack and sometimes uses `malloc()`.
> 
> what you actually asking is compiler that is able to use some AI to see what
> programmer *means* by writing the code. this is not possible.

No. The compiler should be able to detect that a2 is stack allocated (the
compiler should be able to see the type of a2 -> A and also that the memory is
on the stack). Therefore it could call the DTor at the end of the scope. Same
behaviour as it is currently with structs. No AI needed.

But your example with malloc could be true, but I'm sure that the DTor of a2
should be called at least at the end of the programm. The compiler should be
able to do that, even without any AI. ;)

> if you want stack-allocated classes with automatic dtors, why don't use
> `std.typecons.scoped`?

It is IMO not well implemented. I would rather go with the built in scope
(though I know that it is suggested for depreciation).

--


More information about the Digitalmars-d-bugs mailing list