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?
Ioannis
--
* Ioannis Vranos
* Programming pages: http://www.noicys.f2s.com
* Alternative URL: http://run.to/noicys
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
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.
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;
}
"Ioannis Vranos" <noicys@no_spam.yahoo.com> wrote in message
news:10074065...@athprx02.forthnet.gr...
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).
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".
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...
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
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()
{
...???
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);
// ...