Variable to take type of templated class with variable template parameter

D

David Sanders

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.
 
J

James Kanze

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.
 
Z

Zeppe

David said:
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
 
T

terminator

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.
 
D

David Sanders

David said:
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.
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top