template and container, looking for advice

From: Istvan Buki (istvan.buki_at_euronet.be)
Date: 10/08/03


Date: Wed, 08 Oct 2003 13:55:39 +0200

Dear C++ gurus,

I'm having some problems inserting some template classes into containers.
I'm looking for ideas, suggestions,... on how to achieve this but first let
me explain my problem. Everything starts with a simple template class like
the one below. In this example class A has two template parameters but that
number can be different.

    template < typename T, typename U >
    class A
    {
    public:
      void a_method( T *t ) ;

      // Other methods...

    private:
      // private stuff...
    } ;

Now let's say that in the application using class A, I instantiate A with 10
different values for the T template parameter and I create one object of
each type.

To keep track of all these objects I would like to put them in a stl
container but I can't because all the objects have a different type.

I thought about creating a base class and make A derive from it but it
doesn't work because of the "a_method" that depends on one of the template
parameter and I cannot create a pure virtual a_method in the base class.

Then I thought about changing the signature of the "a_method" to something
like this:

          void a_method( BaseT *t );

and make sure that template parameter T is derived from BaseT but it is not
what I need because if I reuse class A in another application I would be
forced to derive all classes used as T template parameter from BaseT which
is probably not meaninful in the context of the new application.

Finally I thought about doing the following:

    template < typename BaseT >
    class ParentA
    {
    public:
      typedef BaseT BaseType ;

      virtual void a_method( BaseT *t ) = 0 ;

      // Other pure virtual methods...
    } ;

    template < typename MyParent, typename T, typename U >
    class A : public MyParent
    {
    public:
      void a_method( MyParent::BaseType *t ) ;

      // ...
    } ;

Then using it like this for example:

    struct SomeBaseClass
    {
      // ...
    } ;

    struct SomeDerivedClass : public SomeBaseClass
    {
      // ...
    } ;

    typedef ParentA< SomeBaseClass > ParentExample ;
    typedef A< ParentExample, SomeDerivedClass, SomeUType > ;

This works but it is not very nice and it forces the user of the class to
code a lot of stuff he should not worry about.
And here I'm stuck. Does anybody have some ideas about how to achieve the
same goal but in cleaner (possibly simpler) way ?

Thank you for your help,
Istvan



Relevant Pages

  • Re: name binding from one template to another
    ... and could only be resolved when instantiation is done. ... The definition of the term "dependent name" in my copy of the ... Compilers routinely parse template definitions without knowing all the ... template parameter T which the template is instantiated with. ...
    (microsoft.public.vc.language)
  • Re: Inheritance & Template parameters
    ... > I would like to apply inheritance to a template parameter, ... How is this going to compile? ... What's so bad if it's a functor of the base class and not ...
    (comp.lang.cpp)
  • Re: Hard to visualize this statement
    ... Queue is a template class taking one template parameter. ... complexis itself a template instantiation. ...
    (microsoft.public.vc.language)
  • Templates and typedef
    ... having trouble when I started to use default template parameters. ... [_Row is a template parameter in another header, ... How can another unrelated template parameter ... what is the scope of the template parameter name? ...
    (comp.lang.cpp)
  • Re: Teaching 5th graders
    ... with it using yogurt containers as molds (did you know that 8oz containers are ... To get the clay off of the outside of the yogurt ... Another tip is to use "tarpaper" to ... Your template will ...
    (rec.crafts.pottery)