You can use structs for this kind of problems, somethin like
struct Scoped(T)
{
~this()
{
//handle deallocation here
}
T inst;
}
if you create a Scoped!MyClass in a scope, the struct's destructor
will be invoked whenever the program leaves the scope, even if it is
because of an exception.
On Tue, Jan 31, 2012 at 2:40 PM, Zachary Lund <admin computerquip.com> wrot=
e:

I've been looking into (basic) memory management within D. Through IRC
conversation and reading the article on memory management on dlang.org
(which seems to be a bit out-of-date), I've concluded that using a global
(or static member) function and emplace() in std.conv is a simple solutio=

n

for providing alternative allocation methods. However, I cannot, by defau=

However, I cannot, by default, scope my custom allocations.
Any ideas?

std.typecons.scoped

I looked into this and I'm unsure of its exact use. It says,
"Allocates a class object right inside the current scope" which
doesn't really define how it's allocated nor does it explain how
this would work with custom de/allocators. Also, it claims it
"avoids" the overhead of new of which I'm not entirely sure of
what it means. Could some clarification be made?

However, I cannot, by default, scope my custom allocations.
Any ideas?

std.typecons.scoped

I looked into this and I'm unsure of its exact use. It says, "Allocate=

s =

a class object right inside the current scope" which doesn't really =

define how it's allocated nor does it explain how this would work with=

=

custom de/allocators. Also, it claims it "avoids" the overhead of new =

of =

which I'm not entirely sure of what it means. Could some clarification=

=

be made?

If scoped is used inside a struct or class, it allocates the space withi=
n
that struct or class. If used inside a function, the space is allocated
on the stack. The size of the allocated buffer must be known at compile
time, so you may not put an instance of a subclass into a scoped variabl=
e.
Because the class may be allocated on the stack, returning it from a
function is a Bad Idea=E2=84=A2, unless it's part of a struct or class y=
ou know
will survive for a while longer.
The reason it avoids the overhead of new is it does not allocate using
new. Basically, Scoped does this:
class A {}
A scoped =3D cast(A)alloca(sizeof(A));
A.__ctor(args);