In C ++, the following code gives a compiler error:
void destruct1 (int * item) { item->~int(); }
This code is almost the same, I just type int on a different type and something magical happens:
typedef int myint; void destruct2 (myint * item) { item->~myint(); }
Why does the second code work? Does int get the destructor just because it was typed?
In case you are wondering why it would ever be desirable to do this: it comes from refactoring C ++ code. We remove the standard heap and replace it with homemade pools. This requires a challenge from us - a new one and destructors. I know that calling destructors for primitive types is useless, but we want them to be in the code if we later replace the POD with real classes.
Finding out that the bare int does not work, but the typed commands were a surprise.
Btw - I have a solution that includes boilerplate functions. We just print inside the template, and everything is fine.
c ++ constructor destructor typedef
Nils Pipenbrinck Jan 19 '09 at 1:43 2009-01-19 01:43
source share