Re: Making a smart pointer which works with incomplete types
* Kai-Uwe Bux:
Maybe I can ease you bewilderment a little. The way I understood Juha's
challenge is to write a drop in replacement for the following template
(which is viewed as a little too bulky):
#include <tr1/memory>
template < typename T >
class smart_ptr {
std::tr1::shared_ptr<T> t_ptr;
public:
smart_ptr ( T* ptr = 0 )
: t_ptr ( ptr )
{}
smart_ptr ( smart_ptr const & other )
: t_ptr ( other.t_ptr )
{}
T & operator* ( void ) const {
return ( *t_ptr );
}
T * operator-> ( void ) const {
return ( t_ptr.operator->() );
}
};
Note that smart_ptr<X> will work as expected as long as X is complete at the
point in the program where smart_ptr<X> is constructed (TR1 guarantees
that).
If you're able to assign to a static pointer to destroy function, then you're
able to have that function as template parameter.
The static pointer buys you nothing whatsoever.
It's just nonsensical complexity.
"Drop in replacement" is supposed to mean that the alternative
implementation should behave like the provided implementation in all cases
(I would like to say in all test cases, but we were not given any).
I think the implementation you suggest falls short because it
requires "deleteX" do be defined.
It doesn't.
(Whether you call that a conceptual
requirement, whether it creates overhead or not, all that is immaterial:
important is just that it allows one to write a test case where your
implementation behaves observably different.)
Given any concrete example of a general principle you can nearly always find
another application of the same general principle where the concrete example is
less than ideal. In this case you've found a more constrained, a more
specialized case (where the type is complete at instantiation point) which can
be solved more *easily*. And complain that the offered example was too general!
I think we've played that game before.
It's silly to the extreme.
Cheers,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?