|
Boost : |
From: Larry Evans (cppljevans_at_[hidden])
Date: 2004-01-21 12:26:45
On 01/21/2004 04:01 AM, Bronek Kozicki wrote:
> On Tue, 20 Jan 2004 22:05:33 -0500, Howard Hinnant wrote:
[snip]
>
> Ahhh, clause 5.3.5/3 : "In the second alternative (delete array) if the
> dynamic type of the object to be deleted differs from its static type,
> the behavior is undefined". Hm. I missed it [1].
>
> However I still think that my example (from
> http://b.kozicki.pl/cpp/T192.cpp ) :
> auto_ptr<Base> source6 ()
> {
> printf("returning pointer to table\n");
> return auto_ptr<Derived>(new Derived[3], del_array());
> }
>
> is valid. This is not really useful application of custom deleter, but
> does not intruduce undefined behaviour, nor unexpected behaviour, thus
> (being quite useless) it's still valid. Please note that deleter will
> always receive static-type == dynamic-type, when used properly (ie.
> smart pointer intialized directly with raw pointer).
[snip]
I BRIEFLY looked at T192.cpp and T191.hpp. T191.hpp shows the
auto_ptr contains 3 pointers:
typedef void (* _free_t_) (const volatile void *);
X* ptr_;
const volatile void* ptv_;
_free_t_ del_; // pointer to deleter function
I believe, at the same memory cost and a restriction of the "initial"
CTOR arguments, auto_new (as the initial CTOR argument) with a 2
template argument auto_ptr, can achieve the same results. The
auto_new I'm referring to is in:
http://groups.yahoo.com/group/boost/files/shared_cyclic_ptr/
auto_smart_ptr.zip
The auto_smart_ptr class is specialized for vectors, much like
the move_ptr<T[]>, but it uses a vector_ptr<T> instead to assure the
argument to the auto_smart_ptr CTOR is actually a vector. Similary,
the auto_smart_ptr for non-vector uses auto_new to assure the argument
the auto_smart_ptr CTOR is actually created from the heap and actually
points to the beginning of the object from the heap. The overhead for
auto_smart_ptr<vector_ptr<T> > is 0. It doesn't allow conversion to
any auto_smart_ptr<vector_ptr<S> > for some superclass S of T (not
tested). The general template auto_smart_ptr takes as CTOR auto_new
which has overhead of 1 pointer in each auto_smart_ptr and 1 pointer
(for virtual function table) in each referent. Since this virtual
function table contains the correct DTOR, the general template can be
used with multi-inherited object. In addition, for T = vector<U>,
conversions are allowed to vector<V> for some supertype of U because
the conversions are by c++ (not tested).
BTW, I think these questions are related to the question of whether
the storage_policy and ownership_policy of policy_ptr should be
merged.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk