Topic: Smart ptr problem with STL
Author: ray@cse.ucsc.edu (Ray Swartz)
Date: 1996/05/14 Raw View
I am using g++ 2.7.2. As far as I can tell, it has no auto_ptr
type. Instead, I create a SmartPtr type to do the same thing.
The copy ctor for SmartPtr is
template <class T>
SmartPtr<T>::SmartPtr(const SmartPtr<T>& x)
{
p = x.p;
x.p = 0;
}
When I try to put a SmartPtr in an STL list, I get an error
that refers to this line in defalloc.h
template <class T1, class T2>
inline void construct(T1* p, const T2& value) {
new (p) T1(value);
}
The error is one where the compiler can't find a match to the ctor
T1(value).
Since value is a constant reference argument, the call to
SmartPtr's copy ctor won't work unless the copy ctor's argument
is a constant reference. This isn't possible because SmartPtr's
copy ctor has to change its argument.
When I rewrite SmartPtr so that its copy ctor can take a constant
reference argument (which means removing the dtor, as well and
rendering the class completely worthless), the program
compiles fine!
The obvious fix is to change the construct function above to
take a T2& instead of a const T2&. I don't have permission
to write to this file so I can't make that change (who
knows what problems this might cause!)
My question is this...
Is there something I am missing here? Is there a correct
way to do this without having to make SmartPtr brain dead?
Thanks,
Ray Swartz
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]