473,320 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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


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
Jan 9 '08 #1
2 1897
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 Tclass 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
Jan 9 '08 #2

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 Tclass 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
Jan 10 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Ann Huxtable | last post by:
I have the following code segment - which compiles fine. I'm just worried I may get run time probs - because it looks like the functions are being overloaded by the return types?. Is this Ok: ? ...
4
by: Ced | last post by:
Hi, i'm not an expert in C but i try to compile BTNG software under linux kernel 2.4.2-2. I get these errors at the very first stage. Does someone could have a rapid look on this and tell me...
26
by: Michael McGarry | last post by:
Hi, I am pretty sure this is not possible, but maybe somehow it is. Given a variable, can I tell what type it is at runtime? Michael
0
by: Jigar.Patel | last post by:
I have simple remoting server exposing following simple method. When I try to add webreference to this server in another project by serveraddresss?wsdl, it gives me following error: Custom tool...
0
by: Jigar.Patel | last post by:
I have simple remoting server exposing following simple method. When I try to add webreference to this server in another project, it gives me following error: Custom tool error: Unable to import...
9
by: Schraalhans Keukenmeester | last post by:
I have some C functions (with variable length argument lists) that use void pointers as arguments. Is there a way to determine at runtime what type of parameter is actually passed on to the...
5
by: aaragon | last post by:
Hello, I was wondering if it is possible to forward declare a type definition. If so, what is the way to do it? I found a couple of messages where they say it's not possible but there must be a way...
3
by: Atropo | last post by:
Hi, all. As you can see I'm just begining on C. with the tutorial "Sams Teach Yourself C in 24 Hours" as a starter. on aix 5.2 when compiling a helloWorld the gcc throws some warnings the...
1
by: Ahmad Nawaz | last post by:
I have a dropdown list which have some elements. I want to get all the elements of the dropdown list into a variable of type array. Any Idea???
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.