|
Boost : |
From: Rob Stewart (stewart_at_[hidden])
Date: 2005-08-17 08:58:10
From: Jonathan Wakely <cow_at_[hidden]>
> Axter wrote:
> >
> > It does not need a clone member if you apply strict pointer ownership logic,
> > which is what a clone pointer normally does.
>
> But there is nothing to prevent users from misusing it (not even much
> documentation!) I would expect a very prominent warning saying you MUST
> NOT create a clone_ptr from a pointer with a different static type to
> its dynamic type.
You could at least offer a runtime check for misuse by comparing
type_ids for the static and dynamic types:
template <class T>
class clone_ptr
{
public:
template <class U>
clone_ptr(U * p)
{
BOOST_ASSERT(typeid(U) == typeid(p));
}
};
You can decide whether that test should occur in all builds.
> > If you create the object by passing it directly to the constructor, it will
> > be able to clone itself with no problems.
>
> I realise that, but it means you can't use clone_ptr in many situations.
>
> The following code applies strict pointer ownership, but is still wrong:
>
> struct Base { ~Base() {} };
>
> std::auto_ptr<Base> factory();
>
> int main() {
> std::auto_ptr<Base> ap = factory();
> clone_ptr<Base> cp(ap.get()); // slice!
> ap.release();
> }
The suggestion above won't detect that at compile time, but it
will detect it.
-- Rob Stewart stewart_at_[hidden] Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk