🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1703

Re: overloaded new/delete

#1703
From: Scott Meyers
Date: Thu, 10 Aug 1995 05:32
67 lines
3110 bytes
Steve Clamage wrote:

>| It is covered. There is no syntax for getting an overloaded operator delete
>| via a delete-expression. You can define a "placement delete", but it is
>| called only if
>|  - a constructor invoked from a placement-new expression exits via an exception, or
>|  - you call the operator delete explicitly.
>| That is:
>| 	T* p ...
>| 	delete p; // never calls a placement delete
>| 	// but you can do this
>| 	p->~T(); // call destructor
>| 	operator delete(p, &allocator);

I asked:

  Okay, what am I missing?  DWP 3.7.3.2 says that if operator delete has a
  second parameter, it must be of type size_t.   

Steve replied:

  My copy of the 28 April 1995 draft doesn't say that. It says a class
  member operator delete (which the above example isn't) MAY have a
  second parameter of type size_t. I don't see anything that says that
  the second parameter if present must have type size_t. See also 5.3.4.

Here's the DWP paragraph in question (3.7.3.2, para 2):

  Each deallocation function shall return void and its  first  parameter
  shall  be  void*.   For  class member deallocation functions, a second
  parameter of type size_t may be added. If both versions  are  declared
  in  the  same  class, the one-parameter form is the usual deallocation
  function and the two-parameter  form  is  used  for  placement  delete
  (_expr.new_).  If the second version is declared but not the first, it
  is the usual deallocation function, not placement delete.

I agree that this paragraph can be interpreted as Steve has, though I
think it could be a lot clearer, especially in view of the fact that
this is a change from past behavior;  ARM 12.5 states,

  Only one operator delete() may be declared for a single class;  thus
  operator delete() cannot be overloaded.  The global operator delete()
  takes a single argument of type void*.
  
Is the following correct?

  - Non-class operator delete can take any number and types of
    arguments as long as the first argument is of type void*.

  - There may be at most two operator delete functions declared in a
    class, and they must have these signatures:
    
      void X::operator delete(void*);
      void X::operator delete(void*, size_t);
      
Finally, why is the obvious syntax for "placement delete" not allowed?
The committee must surely have considered and rejected it.  That is,
why is this not legal for the example above that started this thread?

  delete (&allocator) p;

The restriction against this seems even more glaring than usual in view
of the committee's deliberate introduction of "placement delete" for
handling deallocation when an exception is thrown during initialization
of a dynamic allocation. 

Scott
Technical Details
Message-ID: <smeyersDD2y1C.9nn@netcom.com>
Path: arc1-ca.pugleaf.net!netcom.com!smeyers

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c++
Path: arc1-ca.pugleaf.net!netcom.com!smeyers
From: smeyers@netcom.com (Scott Meyers)
Subject: Re: overloaded new/delete
Message-ID: <smeyersDD2y1C.9nn@netcom.com>
Organization: NETCOM On-line Communication Services (408 261-4700 guest)
Date: Thu, 10 Aug 1995 05:32:00 GMT
Sender: smeyers@netcom20.netcom.com