Connecting Tech Pros Worldwide Help | Site Map

Design of pointer_to<T> templates

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 05:58 PM
Kai-Uwe Bux
Guest
 
Posts: n/a
Default Design of pointer_to<T> templates

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

  #2  
Old July 22nd, 2005, 05:58 PM
Daniel T.
Guest
 
Posts: n/a
Default Re: Design of pointer_to<T> templates

Kai-Uwe Bux <jkherciueh@gmx.net> wrote:
[color=blue]
> I have determined that replacing T* by pointer_to<T> is a good idea:
>
> Any ideas and advice will be highly appreciated.[/color]

Use loki::SmartPtr
<http://sourceforge.net/projects/loki-lib/>
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.