CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2010
    Posts
    54

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Determine the type in the function template

    Why do you want to do this? Why not say, specialise the function template?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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.

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Determine the type in the function template

    Quote Originally Posted by 2kaud View Post
    However, to determine the type of a variable use the typeid operator. See
    http://msdn.microsoft.com/en-us/libr...=VS.80%29.aspx
    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).

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Determine the type in the function template

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Oct 2008
    Posts
    1,456

    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
  •  





Click Here to Expand Forum to Full Width

Featured