Finding and invoking struct destructors in D2

div0 div0 at users.sourceforge.net
Thu May 27 10:54:03 PDT 2010


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Pillsy wrote:
> Hi, all,
> 
> I was wondering if there's any way to determine at compile time
> whether a struct has a (non-trivial) destructor associated with it,
> and whether there's any way to call that destructor without using
> the delete operator. It seems like being able to do these two
> things would allow you to make a container that plays well with
> structs that are designed to do (say) reference counting, but I
> don't see anything in the spec on the website that shows how this
> might be accomplished.
> 
> Thanks!
> Pillsy

Struct and class destructors are called __dtor.
This is undocumented though and therefore not guaranteed to work in
future versions or other(!) implementations:

struct foo {
	~this()
	{
	}
}

int main(){

	foo	b;

	static if(__traits(compiles, b.__dtor()))
	{
		pragma (msg, "got destructor");
	}
	else
	{
		pragma (msg, "no destructor");
	}

	return 0;
}


There are various struct destructor bugs still open though, so I
wouldn't count on being able to do a ref counted objects at the moment:

Most important is this one, which scuppers any change of doing a shared
ptr like struct:

http://d.puremagic.com/issues/show_bug.cgi?id=3516

- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iD8DBQFL/rG7T9LetA9XoXwRAkcyAJ9e/nXmNanAC9L4m6oCRbSk+EruzACfRgOJ
j0xFjV7F4Onz3rSFXeUkq+k=
=1nFQ
-----END PGP SIGNATURE-----


More information about the Digitalmars-d-learn mailing list