Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

templates and run time

0 views
Skip to first unread message

Daniel Fortin

unread,
Dec 3, 2001, 1:59:04 PM12/3/01
to
It's my understanding that the template type is determined at compile time
Ex:

template <class T>
class foo
{..};

main()
{
foo<int> example;
}

Is there anyway to specify the type at run time instead? Something like

if (bFloat)
foo<float> example;
else
foo<int> example;

Thanks

Ioannis Vranos

unread,
Dec 3, 2001, 2:09:50 PM12/3/01
to
"Daniel Fortin" <gaycho...@netzero.net> wrote in message
news:a8283d9b.01120...@posting.google.com...


This is what you did, didn't you?


Ioannis
--
* Ioannis Vranos
* Programming pages: http://www.noicys.f2s.com
* Alternative URL: http://run.to/noicys

Victor Bazarov

unread,
Dec 3, 2001, 2:14:09 PM12/3/01
to
"Daniel Fortin" <gaycho...@netzero.net> wrote...

> It's my understanding that the template type is determined at compile time
> Ex:
>
> template <class T>
> class foo
> {..};
>
> main()

int main()

> {
> foo<int> example;
> }
>
> Is there anyway to specify the type at run time instead? Something like
>
> if (bFloat)
> foo<float> example;
> else
> foo<int> example;

Well, maybe. Depends on what you want. You could create some
kind of polymorphic structure that could be generated based on
a type, like (pseudocode follows):

struct Base { virtual ~Base() {} virtual void foo() = 0; };
template<class T> struct Derived : Base
{
void foo() { cout << "foo()"; }
};

int main()
{
Base *pb;
if (bFloat)
pb = new Derived<float>;
else
pb = new Derived<int>;

pb->foo();
delete pb;
}

Of course, I don't know if that's what you need, since you
didn't say what you needed (that is, you didn't say what problem
you were trying to solve).

Victor
--
Please remove capital A's from my address when replying by mail


Victor Bazarov

unread,
Dec 3, 2001, 2:15:07 PM12/3/01
to
"Ioannis Vranos" <noicys@no_spam.yahoo.com> wrote...

> "Daniel Fortin" <gaycho...@netzero.net> wrote in message
> news:a8283d9b.01120...@posting.google.com...
> > It's my understanding that the template type is determined at compile
> time
> > Ex:
> >
> > template <class T>
> > class foo
> > {..};
> >
> > main()
> > {
> > foo<int> example;
> > }
> >
> > Is there anyway to specify the type at run time instead? Something
> like
> >
> > if (bFloat)
> > foo<float> example;
> > else
> > foo<int> example;
> >
> > Thanks
>
>
> This is what you did, didn't you?


But it's more or less useless, since 'example' cannot be used
beyond that 'if-else' statement...

Ioannis Vranos

unread,
Dec 3, 2001, 2:26:30 PM12/3/01
to
"Victor Bazarov" <vAba...@dAnai.com> wrote in message
news:%aQO7.827$He.1...@sea-read.news.verio.net...

> > > Is there anyway to specify the type at run time instead? Something
> > like
> > >
> > > if (bFloat)
> > > foo<float> example;
> > > else
> > > foo<int> example;
> > >
> > > Thanks
> >
> >
> > This is what you did, didn't you?
>
>
> But it's more or less useless, since 'example' cannot be used
> beyond that 'if-else' statement...

If he does the required jobs within the constructors, then the code
works. If he needs to use these variables later he can create templates
accepting the types he wants and produce the necessary object type. Or
at the worst case scenario he can use the dark forces: macros.

Sekhar

unread,
Dec 3, 2001, 2:46:35 PM12/3/01
to

"Daniel Fortin" <gaycho...@netzero.net> wrote in message
news:a8283d9b.01120...@posting.google.com...

You can use typeid function to get the run-time type identification.

I think you can do something like this

if (strcmp(typeid(var).name(), "int") == 0)
{
foo <int> example ;
else if (strcmp(typeid(var).name(), "float" )== 0);
foo <double> example;
}

Daniel Fortin

unread,
Dec 3, 2001, 2:57:36 PM12/3/01
to
I should have been more specific. The foo object is only available in the
scope of the conditional statement. I'd like to have access to it outside
the if-then-else statement. If I declare an instance of the foo class before
the conditional statement then I need to also specify a type. To get around
this right now I'm just cutting and pasting the code after the declaration
into both conditions of the if statement. I was wondering if there was a
cleaner way to do this.
I hope that's clearer.

"Ioannis Vranos" <noicys@no_spam.yahoo.com> wrote in message
news:10074065...@athprx02.forthnet.gr...

Ioannis Vranos

unread,
Dec 3, 2001, 4:07:27 PM12/3/01
to
"Sekhar" <sumas...@fibercruiser.com> wrote in message
news:9ugjsi$60j$1...@newsreader.mailgate.org...

>
> You can use typeid function to get the run-time type identification.
>
> I think you can do something like this
>
> if (strcmp(typeid(var).name(), "int") == 0)
> {
> foo <int> example ;
> else if (strcmp(typeid(var).name(), "float" )== 0);
> foo <double> example;
> }

We must mention here that this code is not portable, the C-style string
is system dependent. Your code would be portable if it was:

if(typeid(var)==typeid(int))
{
// ...
}

else // ...


but this has the same defficiencies as his original code (created
objects have local scope).

Ioannis Vranos

unread,
Dec 3, 2001, 4:09:27 PM12/3/01
to
"Daniel Fortin" <gaycho...@netzero.net> wrote in message
news:9uglc0$gg6$1...@watserv3.uwaterloo.ca...

> I should have been more specific. The foo object is only available in
the
> scope of the conditional statement. I'd like to have access to it
outside
> the if-then-else statement. If I declare an instance of the foo class
before
> the conditional statement then I need to also specify a type. To get
around
> this right now I'm just cutting and pasting the code after the
declaration
> into both conditions of the if statement. I was wondering if there was
a
> cleaner way to do this.
> I hope that's clearer.


I posted in another message-reply to Victor Bazarov:

"If he does the required jobs within the constructors, then the code
works. If he needs to use these variables later he can create templates
accepting the types he wants and produce the necessary object type. Or
at the worst case scenario he can use the dark forces: macros".

Victor Bazarov

unread,
Dec 3, 2001, 7:04:14 PM12/3/01
to
"Ioannis Vranos" <noicys@no_spam.yahoo.com> wrote...

> "Victor Bazarov" <vAba...@dAnai.com> wrote in message
> news:%aQO7.827$He.1...@sea-read.news.verio.net...
> > > > Is there anyway to specify the type at run time instead? Something
> > > like
> > > >
> > > > if (bFloat)
> > > > foo<float> example;
> > > > else
> > > > foo<int> example;
> > > >
> > > > Thanks
> > >
> > >
> > > This is what you did, didn't you?
> >
> >
> > But it's more or less useless, since 'example' cannot be used
> > beyond that 'if-else' statement...
>
> If he does the required jobs within the constructors, then the code
> works. If he needs to use these variables later he can create templates
> accepting the types he wants and produce the necessary object type. Or
> at the worst case scenario he can use the dark forces: macros.


So, the answer is still "NO" because, unless something changed
since last time I wrote C++ code, macros have nothing to do with
run-time either. He is not really specifying the type at run-time,
he's making the decision about pre-specified types (types, which
were specified at compile-time) during run-time, anyway. I guess
there is no discussion really, since it is based on a wrong premise
that types can be specified during run-time.

Ioannis Vranos

unread,
Dec 3, 2001, 9:28:47 PM12/3/01
to
"Victor Bazarov" <vAba...@dAnai.com> wrote in message
news:2qUO7.849$He.1...@sea-read.news.verio.net...

>
> So, the answer is still "NO" because, unless something changed
> since last time I wrote C++ code, macros have nothing to do with
> run-time either. He is not really specifying the type at run-time,
> he's making the decision about pre-specified types (types, which
> were specified at compile-time) during run-time, anyway. I guess
> there is no discussion really, since it is based on a wrong premise
> that types can be specified during run-time.

Ok now i realised that he mentioned run-time and not compile-time.
Forget about macros, but he can use functions producing the type of
object he likes. I can think situations like this in dynamically linked
programs, and not statically linked ones.

Victor Bazarov

unread,
Dec 4, 2001, 2:47:11 AM12/4/01
to
"Ioannis Vranos" <noicys@no_spam.yahoo.com> wrote...

> "Victor Bazarov" <vAba...@dAnai.com> wrote in message
> news:2qUO7.849$He.1...@sea-read.news.verio.net...
> >
> > So, the answer is still "NO" because, unless something changed
> > since last time I wrote C++ code, macros have nothing to do with
> > run-time either. He is not really specifying the type at run-time,
> > he's making the decision about pre-specified types (types, which
> > were specified at compile-time) during run-time, anyway. I guess
> > there is no discussion really, since it is based on a wrong premise
> > that types can be specified during run-time.
>
> Ok now i realised that he mentioned run-time and not compile-time.
> Forget about macros, but he can use functions producing the type of
> object he likes. I can think situations like this in dynamically linked
> programs, and not statically linked ones.


Well, I've never heard of functions "producing types". If you
could enlighten me, I'll be most appreciative. Or, you could
show me how such functions could be used with templates. And
don't forget, we're in the realm of strictly typed language...

James S. Adelman

unread,
Dec 3, 2001, 5:15:21 PM12/3/01
to
Daniel Fortin wrote:

Refactor the code so that everything which uses the variable example is in
a template function. Then you can have:

template<class T>
void foofunction()
{
foo<T> example;
// ....
}

if(bFloat) foofunction<float>(); else foofunction<int>();

--
James S. Adelman
Liverpool

Ioannis Vranos

unread,
Dec 4, 2001, 8:32:24 AM12/4/01
to
"Victor Bazarov" <vAba...@dAnai.com> wrote in message
news:3c%O7.4190$KY6.25669@rwcrnsc53...

>
> Well, I've never heard of functions "producing types". If you
> could enlighten me, I'll be most appreciative. Or, you could
> show me how such functions could be used with templates. And
> don't forget, we're in the realm of strictly typed language...

I mean type objects. Something like:


class Type1
{};

class Type2
{};

class Type3
{};


template <class T>
Type3 f(T)
{
return Type3();
}

Type2 f(int)
{
return Type2();
}

Type1 f(float)
{
return Type1();

Victor Bazarov

unread,
Dec 4, 2001, 12:00:02 PM12/4/01
to
"Ioannis Vranos" <noicys@no_spam.yahoo.com> wrote...

> "Victor Bazarov" <vAba...@dAnai.com> wrote in message
> news:3c%O7.4190$KY6.25669@rwcrnsc53...
> >
> > Well, I've never heard of functions "producing types". If you
> > could enlighten me, I'll be most appreciative. Or, you could
> > show me how such functions could be used with templates. And
> > don't forget, we're in the realm of strictly typed language...
>
> I mean type objects. Something like:
>
>
> class Type1
> {};
>
> class Type2
> {};
>
> class Type3
> {};
>
>
> template <class T>
> Type3 f(T)
> {
> return Type3();
> }
>
> Type2 f(int)
> {
> return Type2();
> }
>
> Type1 f(float)
> {
> return Type1();
> }

Ioannis, I asked to show how the function are going to be _used_,
not how they are supposed to be implemented. Please, just a bit
more, what's the driver look like? And how is that relevant to
selecting the type at _run-time_. Thank you.

int main()
{
...???

Ioannis Vranos

unread,
Dec 4, 2001, 4:27:38 PM12/4/01
to
"Victor Bazarov" <vAba...@dAnai.com> wrote in message
news:mi7P7.913$He.1...@sea-read.news.verio.net...

> >
> > I mean type objects. Something like:
> >
> >
> > class Type1
> > {};
> >
> > class Type2
> > {};
> >
> > class Type3
> > {};
> >
> >
> > template <class T>
> > Type3 f(T)
> > {
> > return Type3();
> > }
> >
> > Type2 f(int)
> > {
> > return Type2();
> > }
> >
> > Type1 f(float)
> > {
> > return Type1();
> > }
>
> Ioannis, I asked to show how the function are going to be _used_,
> not how they are supposed to be implemented. Please, just a bit
> more, what's the driver look like? And how is that relevant to
> selecting the type at _run-time_. Thank you.

If there is *dynamic* linking (e.g. these are in something like a dll),
you can pass to one of these functions a variable of a certain type, to
get the necessary object.


Something like:


#include "something.dll"

...

int main()
{
Type 3 p=f(2.0);
// ...

0 new messages