Variable to take type of templated class with variable template parameter

Discussion in 'C++' started by David Sanders, Oct 29, 2007.

  1. Hi,

    I have a class depending on a template parameter:

    template<int n>
    class MyClass {
    };

    I want the template parameter to be chosen according to a variable,
    e.g.

    switch (method) {
    case 1:
    pointer = new MyClass<1>;
    break;

    case 2:
    pointer = new MyClass<2>;
    break;

    The question is: what type should 'pointer' be?

    The only thing that occurs to me is to inherit MyClass from a non-
    templated abstract base class:


    class BaseClass {
    };

    template<int n>
    class MyClass : public BaseClass {
    };

    and then define
    BaseClass* pointer;

    This seems to work, but it requires me to define dummy pure virtual
    versions in BaseClass of all the methods in MyClass, which seems like
    something that should be able to happen automatically.

    Is there a simpler / better solution to this problem?

    [The other question is how to make the switch statement automatic, but
    that I guess one does with some kind of factory method?]

    Thanks and best wishes,
    David.
     
    David Sanders, Oct 29, 2007
    #1
    1. Advertising

  2. David Sanders

    James Kanze Guest

    On Oct 29, 8:08 am, David Sanders <> wrote:
    > I have a class depending on a template parameter:


    > template<int n>
    > class MyClass {
    > };


    > I want the template parameter to be chosen according to a variable,
    > e.g.


    > switch (method) {
    > case 1:
    > pointer = new MyClass<1>;
    > break;


    > case 2:
    > pointer = new MyClass<2>;
    > break;


    > The question is: what type should 'pointer' be?


    > The only thing that occurs to me is to inherit MyClass from a non-
    > templated abstract base class:


    > class BaseClass {
    > };


    > template<int n>
    > class MyClass : public BaseClass {
    > };


    > and then define
    > BaseClass* pointer;


    > This seems to work,


    It's also about the only thing that will work. Different
    instantiations of MyClass are unrelated classes; you have to
    explicitly relate them.

    > but it requires me to define dummy pure virtual versions in
    > BaseClass of all the methods in MyClass, which seems like
    > something that should be able to happen automatically.


    Not really. What should this "implicitly defined" base class
    look like if there are specializations of your template?
    Templates and runtime polymorphism are two very different
    things. If you want runtime polymorphism, you must follow the
    rules of runtime polymorphism (even if you use templates in the
    implementation of the derived types).

    The basic reason for this is that runtime polymorphism takes
    place in a different environment than template instantiation.
    Templates can get away with duck typing because if you
    instantiate over a type which doesn't meet the contract, the
    compiler will complain, and the error doesn't go any further.
    Use duck typing a runtime (a la Smalltalk or Lisp), and you risk
    type errors at runtime---require a common base class and
    explicit derivation, and the compiler can catch most of these
    errors.

    > Is there a simpler / better solution to this problem?


    > [The other question is how to make the switch statement
    > automatic, but that I guess one does with some kind of factory
    > method?]


    More or less (although I usually use factory objects). Just put
    the address of the function/object in a map, indexed by the key.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Oct 29, 2007
    #2
    1. Advertising

  3. David Sanders

    Zeppe Guest

    Re: Variable to take type of templated class with variable templateparameter

    David Sanders wrote:
    > Hi,
    >
    > I have a class depending on a template parameter:


    ...

    > Is there a simpler / better solution to this problem?
    >


    No, there isn't. Different instantiation of template classes are not
    related anyhow, so the correct way to link them is in letting them
    derive from a single base class. And, the virtual functions are
    unavoidable if you want polymorphic behaviour.

    > [The other question is how to make the switch statement automatic, but
    > that I guess one does with some kind of factory method?]


    The switch part has to appear somewhere (in the factory, possibly),
    since the template argument has to be resolved at compile-time. However,
    depending on your specific problem you can rely on some class
    auto-registration mechanism.

    > Thanks and best wishes,
    > David.
    >


    Best regards,

    Zeppe
     
    Zeppe, Oct 29, 2007
    #3
  4. David Sanders

    terminator Guest

    On Oct 29, 10:08 am, David Sanders <> wrote:
    > Hi,
    >
    > I have a class depending on a template parameter:
    >
    > template<int n>
    > class MyClass {
    >
    > };
    >
    > I want the template parameter to be chosen according to a variable,
    > e.g.
    >
    > switch (method) {
    > case 1:
    > pointer = new MyClass<1>;
    > break;
    >
    > case 2:
    > pointer = new MyClass<2>;
    > break;
    >
    > The question is: what type should 'pointer' be?
    >
    > The only thing that occurs to me is to inherit MyClass from a non-
    > templated abstract base class:
    >
    > class BaseClass {
    >
    > };
    >
    > template<int n>
    > class MyClass : public BaseClass {
    >
    > };
    >
    > and then define
    > BaseClass* pointer;
    >
    > This seems to work, but it requires me to define dummy pure virtual
    > versions in BaseClass of all the methods in MyClass, which seems like
    > something that should be able to happen automatically.
    >
    > Is there a simpler / better solution to this problem?
    >
    > [The other question is how to make the switch statement automatic, but
    > that I guess one does with some kind of factory method?]
    >
    > Thanks and best wishes,
    > David.


    void * pointer;/*you will be responsible for type of the object*/

    if 'pointer' is not of a polymorphic type you will be in trouble ,so I
    think this is really bad.

    regards,
    FM.

    regards,
    FM.
     
    terminator, Oct 29, 2007
    #4
  5. On Oct 29, 3:36 am, Zeppe
    <> wrote:
    > David Sanders wrote:
    > > Hi,

    >
    > > I have a class depending on a template parameter:

    >
    > ...
    >
    > > Is there a simpler / better solution to this problem?

    >
    > No, there isn't. Different instantiation of template classes are not
    > related anyhow, so the correct way to link them is in letting them
    > derive from a single base class. And, the virtual functions are
    > unavoidable if you want polymorphic behaviour.
    >
    > > [The other question is how to make the switch statement automatic, but
    > > that I guess one does with some kind of factory method?]

    >
    > The switch part has to appear somewhere (in the factory, possibly),
    > since the template argument has to be resolved at compile-time. However,
    > depending on your specific problem you can rely on some class
    > auto-registration mechanism.


    Hi, thanks very much for the helpful replies. I'm happy that I was on
    the right track!

    Best wishes,
    David.
     
    David Sanders, Oct 30, 2007
    #5
    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. case2005
    Replies:
    3
    Views:
    1,813
    Nicolas Pavlidis
    Feb 13, 2005
  2. Amadeus W. M.
    Replies:
    2
    Views:
    399
    Amadeus W. M.
    Jul 4, 2006
  3. Fei Liu
    Replies:
    4
    Views:
    790
    Victor Bazarov
    Oct 26, 2007
  4. Stuart Redmann
    Replies:
    5
    Views:
    483
    Stuart Redmann
    Dec 14, 2007
  5. chhenning
    Replies:
    5
    Views:
    367
    chhenning
    Feb 13, 2008
Loading...

Share This Page