Design of pointer_to<T> templates

K

Kai-Uwe Bux

Hi,


I have determined that replacing T* by pointer_to<T> is a good idea:

(a) If the template signature is actually

template < typename T, typename Alloc=some_standard_allocator<T> >
class pointer_to {
...
};

then the template makes it easy to play with allocators for performance
tuning or for implemeting container classes that conform to the allocator
conventions of the standard library.

(b) Using magic allocators or a different implementation of pointer_to<T>,
one can hunt for memory leaks.

(c) In code like

class B { ... };
class D : public B { ... };

pointer_to<B> b_ptr;
pointer_to<D> d_ptr;

b_ptr = d_ptr;

the compiler can issue an error if class B is not polymorphic.


It is the last point that gives me some design problems. I am considering
the following options:

I) Supply two templates:

typebound_ptr<T,Alloc>
polymorphic_ptr<T,Alloc>

where the first will not allow for polymorphic use and the second will not
compile if T is not polymorphic.

The consequence is that I will have to make up my mind every time as to
whether I intend to use a pointer type polymorphically or not. This may be
an advantage in that it probably improves code quality in most cases.
Sometimes, however, it might not be feasibile to decide beforehand -- I am
thinking of coding templates for instance.

II) Implement pointer_to<T> so that assignment, copy constructor, and
perhaps comparison operators will check whether T is polymorphic if their
polymorphic versions are used.

III) Provide all three pointer templates.

This might allow for maximum flexibilty or for maximum confusion. Moreover,
one might end up converting between these pointer types, which somewhat
defies the purpose.


I have working code for most of this, I am just about to decide which files
to ditch.


Any ideas and advice will be highly appreciated.


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top