Path: utzoo!utgpu!watserv1!watmath!att!dptg!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.std.c++ Subject: Re: Asymmetry: "new (placement) T" vs. "Tptr->~T" Keywords: placement new delete constructor destructor template Message-ID: <11256@alice.UUCP> Date: 30 Aug 90 19:23:27 GMT References: <620@dlogics.COM> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 41 In article <620@dlogics.COM>, jmd@dlogics.COM (Jens M. Dill) writes: > Is anyone else bothered by the inherent asymmetry in the application of > constructors and destructors to an object that is to reside in a known > memory location? The asymmetry is an intrinsic part of the operation. In one case you're taking raw memory and building an object. In the other, you're destroying an object that already exists. > To undo this, one is supposed to apply the destructor ~T explicitly: > Tptr->~T () ; > This is all very well if one knows whether or not T is a class with a > destructor, but consider the case where T is a type argument to a template > (or to a macro that emulates the template facility). In this case, one > cannot know if T is a type with constructors or destructors, since that > information is unavailable until T is bound in a template-class definition. Yes, that is indeed a problem. The original article suggested several solutions, and one of those has actually been written into the manual. See Ellis & Stroustrup, page 280: The notation for explicit call of a destructor may be used for any simple type name. For example: int* p; // ... p->int::~int(); Using the notation for a type that does not have a destructor has no effect. Allowing this enables people to write code without having to know if a destructor exists for a given type. This isn't implemented everywhere yet, but I hope it will be. -- --Andrew Koenig ark@europa.att.com