Design of pointer_to<T> templates

Discussion in 'C++' started by Kai-Uwe Bux, Aug 17, 2004.

  1. Kai-Uwe Bux

    Kai-Uwe Bux Guest

    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
    Kai-Uwe Bux, Aug 17, 2004
    #1
    1. Advertising

  2. Kai-Uwe Bux

    Daniel T. Guest

    Kai-Uwe Bux <> wrote:

    > I have determined that replacing T* by pointer_to<T> is a good idea:
    >
    > Any ideas and advice will be highly appreciated.


    Use loki::SmartPtr
    <http://sourceforge.net/projects/loki-lib/>
    Daniel T., Aug 17, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fred
    Replies:
    1
    Views:
    597
    Neredbojias
    Sep 26, 2005
  2. John Harrison

    using templates in templates

    John Harrison, Jul 31, 2003, in forum: C++
    Replies:
    8
    Views:
    380
    Torsten Curdt
    Jul 31, 2003
  3. JKop
    Replies:
    3
    Views:
    468
  4. Tom McCallum

    Templates within templates

    Tom McCallum, Aug 4, 2004, in forum: C++
    Replies:
    2
    Views:
    364
    tom_usenet
    Aug 4, 2004
  5. recover
    Replies:
    2
    Views:
    800
    recover
    Jul 25, 2006
Loading...

Share This Page