Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Run-time template list definition / Run-time variable type definition

Reply
Thread Tools

Run-time template list definition / Run-time variable type definition

 
 
Pierre Yves
Guest
Posts: n/a
 
      01-09-2008

Hi there,

Sorry for the double subject but I feel they are related. I'm not pretty
sure there would be an answer but I reckon there must be a way to make
it work.

I would like to write the following bit of code:

8<----------------------------

Foo<> * myFoo = NULL; // Foo is a templated class.

if (condition1) {
myFoo = new Foo<double>(parameters);
} else {
myFoo = new Foo<long>(parameters);
}

processFoo(myFoo);

8<----------------------------

This version is obviously illegal. Have you any ideas for an
alternative? i.e. declaring the template list of my templated class at
run time?

I'm looking for a way to do some run-time polymorphism at run-time with
templates. Can I define the template class is derivated from an other
class (similar to an interface in java) or the template will raise
conflicts? Is there a nicer solution?

Thanks for your help,
PY
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-09-2008
Pierre Yves wrote:
> Hi there,
>
> Sorry for the double subject but I feel they are related. I'm not
> pretty sure there would be an answer but I reckon there must be a way
> to make it work.
>
> I would like to write the following bit of code:
>
> 8<----------------------------
>
> Foo<> * myFoo = NULL; // Foo is a templated class.
>
> if (condition1) {
> myFoo = new Foo<double>(parameters);
> } else {
> myFoo = new Foo<long>(parameters);
> }
>
> processFoo(myFoo);
>
> 8<----------------------------
>
> This version is obviously illegal. Have you any ideas for an
> alternative? i.e. declaring the template list of my templated class at
> run time?


You either have a template wrapper around your 'condition1' and
'processFoo' (IOW, make 'processFoo' a template), or create a base
class for all Foo, like so

class FooBase { ... };
template<class T> class Foo : public FooBase { ... };

and then declare 'myFoo' to be a pointer to base:

FooBase * myFoo( condition1 ?
new Foo<double>(parameters) :
new Foo<long>(parameters) );

processFoo(myFoo);

Of course, 'processFoo' would need to be refactored.

> I'm looking for a way to do some run-time polymorphism at run-time
> with templates. Can I define the template class is derivated from an
> other class (similar to an interface in java) or the template will
> raise conflicts? Is there a nicer solution?


Not sure what your doubt is about here. Run-time polymorphism and
templates are _orthogonal_ (except that you cannot define a template
member function and declare it virtual at the same time).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Pierre Yves
Guest
Posts: n/a
 
      01-10-2008

Victor,

thanks for your answer... I found out that I was not able to do a
non-templated base class because I required some templated method from
processFoo. Maybe, my design was poor.

I did in an other way. I created a templated function which is similar
to the following:

8<-------------------------

template<class T>
int OverallProcessFoo(T t, parameters) {
Foo<T> * myFoo = new Foo<T>(parameters);

processFoo(myFoo);

return EXIT_SUCCESS;
}

8<-------------------------

Might not be the cleanest but it does the job if I can call
OverallProcessFoo at the same time I know the outcome of condition1.

Regards,
PY


Victor Bazarov previously wrote the following e-mail:
> Pierre Yves wrote:
>> Hi there,
>>
>> Sorry for the double subject but I feel they are related. I'm not
>> pretty sure there would be an answer but I reckon there must be a way
>> to make it work.
>>
>> I would like to write the following bit of code:
>>
>> 8<----------------------------
>>
>> Foo<> * myFoo = NULL; // Foo is a templated class.
>>
>> if (condition1) {
>> myFoo = new Foo<double>(parameters);
>> } else {
>> myFoo = new Foo<long>(parameters);
>> }
>>
>> processFoo(myFoo);
>>
>> 8<----------------------------
>>
>> This version is obviously illegal. Have you any ideas for an
>> alternative? i.e. declaring the template list of my templated class at
>> run time?

>
> You either have a template wrapper around your 'condition1' and
> 'processFoo' (IOW, make 'processFoo' a template), or create a base
> class for all Foo, like so
>
> class FooBase { ... };
> template<class T> class Foo : public FooBase { ... };
>
> and then declare 'myFoo' to be a pointer to base:
>
> FooBase * myFoo( condition1 ?
> new Foo<double>(parameters) :
> new Foo<long>(parameters) );
>
> processFoo(myFoo);
>
> Of course, 'processFoo' would need to be refactored.
>
>> I'm looking for a way to do some run-time polymorphism at run-time
>> with templates. Can I define the template class is derivated from an
>> other class (similar to an interface in java) or the template will
>> raise conflicts? Is there a nicer solution?

>
> Not sure what your doubt is about here. Run-time polymorphism and
> templates are _orthogonal_ (except that you cannot define a template
> member function and declare it virtual at the same time).
>
> V

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable to take type of templated class with variable template parameter David Sanders C++ 4 10-30-2007 07:33 AM
Template-Meta: Finding out whether a template-definition exists Hendrik Schober C++ 16 09-04-2007 11:48 AM
Template member functions in template class with separate definition =?iso-8859-1?q?Erik_Wikstr=F6m?= C++ 5 12-14-2006 01:04 PM
Out-of-class definition of a non-template member of an explicitly specialized class template? BigMan C++ 3 04-22-2005 06:28 AM
How to pass variable argument list to another function w/ variable argument list? Ben Kial C Programming 1 11-15-2004 01:51 AM



Advertisments