
From: Jonathan Wakely <cow@compsoc.man.ac.uk>
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@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;