TITLE: reasons for allowing delete of incomplete types (Newsgroups: comp.lang.c++.moderated, 20 Nov 96) CLAMAGE: Steve Clamage >> The transformation to C++ code >> would look like this: >> struct T; >> void foo( T* t ) { delete t; } >> Assuming the "malloc" was replaced by "new", and the T has a trivial >> destructor (which must be the case for C), this code must still work. STOKES: Alan Stokes >Why? There was no need to change the existing code, as you said it was >valid C++. If you want to change it to use C++ idioms, then go the whole >hog - in this case you should make sure that the type is complete when >you delete it. CLAMAGE: clamage@taumet.Eng.Sun.COM (Steve Clamage) That was the reason for allowing deletion of a pointer of an incomplete type. I agree it is not necessarily a compelling reason, but consider this: In converting from C to C++ it makes sense to change all uses of malloc/free to new/delete. Since you will want new/delete for class objects, you probably do not want to mix new and malloc in the same program. It is allowed, but makes program maintenance difficult. It is hard to find all instances of free-ing an object that used to be allocated with malloc when you turn the object into a class and want to allocate it with "new". It is far easier to change uniformly all malloc/calloc/realloc/free to new and delete. Making all the changes involves simple and local text editing. The additional change of providing full structure declarations where only forward declarations were used before can involve serious restructuring of the program files. IMHO (you might disagree) that is a different order of magnitude of difficulty. Hence the "C compatibility" argument. OTOH, a compiler is allowed to warn about deleting an object of incomplete type, and many compilers do so. A compiler is also allowed to reject a program which incorrectly deletes an object of incomplete type (e.g. if the compiler somehow knows the completed type has a nontrivial destructor). (I don't know of any compilers that perform that check.)