Thread View: comp.std.c++
2 messages
2 total messages
Started by Scott Meyers
Thu, 10 Aug 1995 05:32
Re: overloaded new/delete
Author: Scott Meyers
Date: Thu, 10 Aug 1995 05:32
Date: Thu, 10 Aug 1995 05:32
67 lines
3110 bytes
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
Re: overloaded new/delete
Author: Bill Gibbons
Date: Thu, 10 Aug 1995 22:21
Date: Thu, 10 Aug 1995 22:21
57 lines
2635 bytes
2635 bytes
In article <smeyersDD2y1C.9nn@netcom.com>, smeyers@netcom.com (Scott
Meyers) wrote:
> 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*.
Yes.
> - 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);
No. There may be any number of delete functions in a class. If present,
the first form above is always the usual delete. Any which matches neither
of these forms is a placemeent delete. If the second form appears, it's
the usual delete if the first form is missing; else it's a placement delete.
This is obviously more complex than one would like, but it's the simplest
scheme which does not require new syntax or keywords.
The wording about not overloading delete is obsolete. When the "delete"
keyword is used, only the non-placement delete (and there is only one of
these) is ever called. But when operator delete is invoked explicitly,
it is just an ordinary function and of course overloading rules apply.
> 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;
But what about:
delete (&allocator) * p;
This is syntactically ambiguous (and it can be made semantically
ambiguous if "p" is an object of a class with both unary and binary
overloaded operator*).
A similar situation occurs with new-expressions:
new int * * x;
The resolution for types is to use as many tokens as possible in the type.
But the situation with types (for "new") is fairly simple, since the
only ambiguities involve trailing "*" and "&". The corresponding
situation with expressions is a little more complex, and the committee
did not consider it worth the added complexity, not to mention the
potential for breaking existing code. Of course you're free to disagree.
--
Bill Gibbons
bgibbons@taligent.com
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads