www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Finding and invoking struct destructors in D2

reply Pillsy <pillsbury gmail.com> writes:
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
May 27 2010
parent reply div0 <div0 users.sourceforge.net> writes:
-----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-----
May 27 2010
next sibling parent Pillsy <pillsbury gmail.com> writes:
== Quote from div0 (div0 users.sourceforge.net)'s article:
[...]
 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

Yeah, that basically kills the idea until the bug is fixed. :( Once it is, I think a hasDestructor template would be a good thing to have in the standard library. Cheers, Pillsy
May 27 2010
prev sibling parent Don <nospam nospam.com> writes:
div0 wrote:
 -----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

Yup, that's #3 on my list of Ten Must-fix Bugs. This related one is pretty bad too.
 http://d.puremagic.com/issues/show_bug.cgi?id=3323

 
 - --
 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-----

May 27 2010