-
July 28th, 2013, 08:47 PM
#1
Determine the type in the function template
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
-
July 28th, 2013, 09:46 PM
#2
Re: Determine the type in the function template
Why do you want to do this? Why not say, specialise the function template?
-
July 29th, 2013, 05:46 AM
#3
Re: Determine the type in 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
Last edited by 2kaud; July 29th, 2013 at 05:48 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 29th, 2013, 06:56 AM
#4
Re: Determine the type in the function template
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.
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.
}
}
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.
-
July 29th, 2013, 07:01 AM
#5
Re: Determine the type in the function template
Originally Posted by 2kaud
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).
-
July 29th, 2013, 08:30 AM
#6
Re: Determine the type in the function template
Originally Posted by OReubens
Writing your template specialisations properly to avoid the whole problem is the way to go.
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.
-
July 29th, 2013, 09:36 AM
#7
Re: Determine the type in the function template
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:
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 );
}
it's easy to see how far something like this can generalize ...
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|