Menu

#178 SmartPtr: DeepCopy and ArrayStorage are not compatible

closed-wont-fix
None
5
2011-09-21
2011-03-19
Anonymous
No

DeepCopy ownership policy and ArrayStorage storage policy are not compatible, because only the first element in array gets copied by Clone() function of the pointee.

#include <Loki/SmartPtr.h>

struct Foo {
Foo* Clone() const { return new Foo(*this); }
};

int main() {
typedef Loki::SmartPtr<Foo, Loki::DeepCopy, Loki::DisallowConversion,
Loki::AssertCheck, Loki::ArrayStorage> Ptr;
Ptr ptr(new Foo[23]);
Ptr ptrCopy(ptr); // only first element is copied
}

I think there should be a partial specialization disallowing this combination of policies.

Discussion

  • Richard Sposato

    Richard Sposato - 2011-09-21

    There are multiple problems with attempting to fix this problem.

    One is that C++ does not allow partial specializations of functions inside template classes. Section 1.4 of Modern C++ Design says "Specialization of member functions does not scale. You can specialize any member function of a template with one template parameter, but you cannot specialize individual member functions for templates with multiple template parameters."

    The second problem has to do with making any array of objects where each element in the new array is a copy of its counterpart in the source array. If we use the array new operator, (e.g. - new T[ itemCount ]; ), that will just call the default constructor of type T - not the copy constructor for each element in the source array.

    I can add a comment to DeepCopy and ArrayStorage policies classes saying that each should not be used with the other. That's probably the best we can do here.

    Cheers,

    Rich

     
  • Richard Sposato

    Richard Sposato - 2011-09-21
    • status: open --> closed-wont-fix
     
  • Richard Sposato

    Richard Sposato - 2011-09-21
    • assigned_to: nobody --> rich_sposato
     

Log in to post a comment.