Topic: Can we check the actual data type in templa
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 21 Nov 1994 17:32:47 GMT Raw View
In article 94Nov18162021@orange.ridgefield.sdr.slb.com, saito@orange.ridgefield.sdr.slb.com (Naoki Saito) writes:
>Hello! I would like to know whether it is possible to check the data type
>in the template constructs. What I want to do can be described roughly
>as follows:
>
>template <class DT>
>void f(DT x)
>{
> switch (datatype of(DT)) // check what the actual DT is in the calling prog.
> {
> case "int":
> task_int(x);
> break;
> case "float":
> task_float(x);
> break;
> case "double":
> task_double(x);
> break;
> }
>}
If your compiler supports RTTI you can do it that way. But needing to check
the type is often a symptom of incorrect design. In your particular example,
you could provide specializations of the template function:
void f(int x) { task_int(x); }
void f(float x) { task_float(x); }
void f(double x){ task_double(x); }
But what about those three "task" functions? Shouldn't they be overloaded:
void task(int);
void task(float);
void task(double);
Or perhaps "task" should be a template function as well:
template <class DT> void task(DT);
Either way, you wouldn't need template specializations, and you wouldn't need
to test the argument type in the function template:
template<class DT>
void f(DT x)
{
task(x);
}
Perhaps none of these solutions fits your specific case. But needing to
check the type at run time is a red flag that should cause you to revisit
your design.
---
Steve Clamage, stephen.clamage@eng.sun.com
Author: jason@cygnus.com (Jason Merrill)
Date: Tue, 22 Nov 1994 01:45:31 GMT Raw View
>>>>> John DiCamillo <milod@netcom.com> writes:
> Huh? When did RTTI start working on non-polymorphic
> types?
typeid() has always worked on non-polymorphic types. Why shouldn't it?
Jason
Author: milod@netcom.com (John DiCamillo)
Date: Mon, 21 Nov 1994 23:11:19 GMT Raw View
clamage@Eng.Sun.COM (Steve Clamage) writes:
>In article 94Nov18162021@orange.ridgefield.sdr.slb.com, saito@orange.ridgefield.sdr.slb.com (Naoki Saito) writes:
>>Hello! I would like to know whether it is possible to check the data type
>>in the template constructs. What I want to do can be described roughly
>>as follows:
>>
>>template <class DT>
>>void f(DT x)
>>{
>> switch (datatype of(DT)) // check what the actual DT is in the calling prog.
>> {
>> case "int": >> task_int(x); >> break;
>> case "float": >> task_float(x); >> break;
>> case "double": >> task_double(x); >> break;
>> }
>>}
>If your compiler supports RTTI you can do it that way.
Huh? When did RTTI start working on non-polymorphic
types?
>But needing to check
>the type is often a symptom of incorrect design. In your particular example,
>you could provide specializations of the template function:
> void f(int x) { task_int(x); }
> void f(float x) { task_float(x); }
> void f(double x){ task_double(x); }
>But what about those three "task" functions? Shouldn't they be overloaded:
> void task(int);
> void task(float);
> void task(double);
>Or perhaps "task" should be a template function as well:
>template <class DT> void task(DT);
But then, *task()* would need to be specialized:
chicken-and-egg problem.
In any case, template specializations or overloaded
functions seem to be the right answer. But (hedging
my bet) another possibility would be to use numeric
*classes*, and rely on (inclusion) polymorphism.
--
ciao,
milo
================================================================
John DiCamillo Pinin' for the fjords?
milod@netcom.com What kind of talk is that?
Author: milod@netcom.com (John DiCamillo)
Date: Tue, 22 Nov 1994 22:51:18 GMT Raw View
jason@cygnus.com (Jason Merrill) writes:
>>>>>> John DiCamillo <milod@netcom.com> writes:
>> Huh? When did RTTI start working on non-polymorphic
>> types?
>typeid() has always worked on non-polymorphic types. Why shouldn't it?
Mea culpa: I stand corrected.
I always thought that typeid worked
just like dynamic_cast. Now I know.
>Jason
--
ciao,
milo
================================================================
John DiCamillo Pinin' for the fjords?
milod@netcom.com What kind of talk is that?