|
Boost : |
From: Daryle Walker (darylew_at_[hidden])
Date: 2000-08-22 13:42:14
on 8/21/00 8:31 PM, David Abrahams at abrahams_at_[hidden] wrote:
> No opinion on whether we should have this or not, but just let me point out
> that it is very different from noncopyable in two important respects:
>
> 1. noncopyable can be used when no sensible copy semantics exist. There is
> simply no analogue for dynamic construction. If it makes sense to write
> X x;
> then it also makes sense to say
> std::auto_ptr<X> x(new X);
Why does being able to make an auto variable of that type semantically imply
that an auto_ptr'd dynamic version should also be allowed?
> 2. noncopyable prevents copying even when the type in question is a
> subobject. nonnewable can't prevent dynamic allocation as a subobject.
I looked that Strostrup's C++ book (third edition), and I thought it implied
that a derived class will use its base class's operator new when a derived
class object gets dynamically allocated (assuming that the base class has
such an operator available and the derived class doesn't have its own). So
I took that to mean that a base class that blocks operator new forbids that
operator for its derived classes, unless the derived classes make a custom
version.
> P.S. No, the destructor shouldn't be virtual.
I thought so, but I wasn't sure. Normally, it should be virtual if a custom
operator delete is defined, so the dynamic dispatch stuff can figure out the
size of the true object to pass to the operator. In my case, that size
calculation doesn't need to be done since operator delete is blocked from
use.
> P.P.S. nonnewable doesn't prevent the use of placement new into dynamically
> allocated memory
Another response, from Marshall Clow, mentioned this:
> There's also "placement new", which this doesn't prevent.
> void* operator new ( std::size_t, void* ptr );
Couldn't I define a private placement-new operator with this signature?
That would block the most common custom new. It wouldn't block any other
customized new, but those are rare.
P.S. Mr. Clow said my idea was in one of Meyers' books (I don't have it).
> Didn't Scott Meyers talk about this?
> <flip, flip, flip, aha! More Effective C++, item #27>
P.P.S. [OT] What would happen if a class used multiple inheritance and at
least two base classes had customized operator new functions.
--
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk