I have an auto pointer class and in the constructor I am passing in a pointer. I want to be able to separate new from new[] in the constructor so that I can properly call delete or delete[] in the destructor. Can this be done through template specialization? I don’t want to have to pass in a boolean in the constructor.
template <typename T>
class MyAutoPtr
{
public:
MyAutoPtr(T* aPtr);
};
// in use:
MyAutoPtr<int> ptr(new int);
MyAutoPtr<int> ptr2(new int[10]);
std::unique_ptrin C++0x will have a specialization for dynamic arrays, somewhat like shown below. However, it will be the user’s task to instantiate an appropriate instance. At language level there is no way to distinguish one pointer from another.Furthermore, it might not be that good to load one class with so various tasks. For example, boost has
shared_ptrandshared_array.