Hi,
I want to detect the type in a function template, like this:
template <class myType> myType Function (myType a, myType b) {
// Detect the myType
If (myType is int)
{
…
}
Else
{
…
}
}
Is that possible?
Thanks
Alan
Printable View
Hi,
I want to detect the type in a function template, like this:
template <class myType> myType Function (myType a, myType b) {
// Detect the myType
If (myType is int)
{
…
}
Else
{
…
}
}
Is that possible?
Thanks
Alan
Why do you want to do this? Why not say, specialise the function template?
One of the purposes of using templates is that you shouldn't need to know the types of the function parameters. I would be looking closely at your program design in cases like this.
However, to determine the type of a variable use the typeid operator. See
http://msdn.microsoft.com/en-us/libr...=VS.80%29.aspx
As above pointed out, this is going somewhat against the reason of templates, which means you have code that will generically work for any type of template parameter you pass in.
But yes, there are cases where you will want to alter how something works for some types, and this is called specialisation (or partial specialisation)
Code:// Generic code, adds a and b
template <class myType>
myType Function(myType a, myType b)
{
return a+b;
}
// specialisation for int subtracts b from a
template <>
int Function<int>(int a, int b)
{
return a-b;
}
int main()
{
double d = Function(5.0, 3.0); // return 8.0
int i = Function(5, 3); // return 2
}
Now of course, you can use the above to make a type test as well, although this may result in unoptimal code, or even code that doesn't compile if the branch-not-taken doesn't 'work' for the type.
Note that the compiler will typically be able to "optimize away" the branch not taken, but even if so, it still needs to be able to compile that branch in the first place.Code:// generic template
template <class T>
bool isint()
{
return false; // generic types aren't int
}
// specialize for int
template <>
bool isint<int>()
{
return true; // only an int is an int
}
template <class myType>
myType Function(myType a, myType b)
{
if (isint<myType>())
{
//Do Int stuff
return a-b; // This wil fail to compile if myType doesn't have a binary + operator.
}
else
{
//Do non-int stuff
return a.GetValue() + b.GetValue() // this won't compile for int, it will compile for types that have a GetValue() that is addable.
}
}
Correct, but...
This causes actual comparison code to be generated, I doubt the compiler will be able to optimize the test or the branch not taken in this case.
The template-based test I posted before this is a better solution, although it's still somewhat of a kludge. Writing your template specialisations properly to avoid the whole problem is the way to go. (but I've been in situations where i had to do something like the above anyway, so it's not entirely pointless).
Plus, it is a far more well known and expected solution for the cases when you really need to um, special case a function template for a particular type.Quote:
Originally Posted by OReubens
ditto what others said, but sometimes you do need more flexible and predictable techniques than specialization or overloading. For example, with tag dispatching you can write things like:
it's easy to see how far something like this can generalize ...Code:namespace detail
{
template< typename T >
struct FooTag { };
template<>
struct FooTag<int> { };
int FooImpl ( FooTag<int>, int a, int b )
{
// ...
}
// ...
}
template <class myType> myType Foo (myType a, myType b)
{
detail::Fooimpl( FooTag<MyType>, a, b );
}