In template function, how query the type?

This is a discussion on In template function, how query the type? within the C++ Programming forums, part of the General Programming Boards category; If I have a function with a template type, how can I query that at runtime? For example, if I ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    In template function, how query the type?

    If I have a function with a template type, how can I query that at runtime? For example, if I want to know if it's a primitive (int, long, bool, etc) I'll do one thing, otherwise something else.

    Code:
    template <typename T>
    void getData(const std::vector<T> &data)
    {
          //I essentially want to do something like this
         if ( type_id( T ) == type_id( int ) ...
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    One option is to specialise the template.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Often you want the code to work regardless of type.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    I got it to work!

    Code:
    if ( typeid( T ) == typeid( int ) )...

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    I got it to work!
    If you can avoid RTTI, avoid it. If you specialise the template, there is no RTTI overhead (though you may add to executable code bloat).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Banned
    Join Date
    Nov 2007
    Posts
    678
    laserlight: why is RTTI so horrible?
    i understand that in some applications, due to real time needs, you may be concerned.
    but why should we avoid it, in general, for all kind of apps?

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by manav View Post
    laserlight: why is RTTI so horrible?
    i understand that in some applications, due to real time needs, you may be concerned.
    but why should we avoid it, in general, for all kind of apps?
    Using RTTI in a template defeats the entire purpose of templates, which is that you can specialize on specific types.

  8. #8
    Banned
    Join Date
    Nov 2007
    Posts
    678
    ok. so laserlight was saying that avoid RTTI in case of templates.
    i thought he was saying avoid RTTI in general. always.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    i thought he was saying avoid RTTI in general. always.
    That is what I meant. Of course, "avoid ... always" is not correct, since situations may arise where RTTI is the best solution, given other factors. In more general cases, the kinds of problems for which RTTI may be a solution would be better solved with templates or virtual functions.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Banned
    Join Date
    Nov 2007
    Posts
    678
    laserlight: i just read some info about RTTI. and yes, we don't need to avoid RTTI. in fact it is just not needed. always.
    and it's much easier to use alternatives like templates and virtuals.
    actually till now i never had to care about it.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    No, it is needed in certain situations. Don't blam is just because you haven't had the need for it or because someone else just says so.
    They say the same about multiple inheritance, but it isn't necessarily bad (I have used it plenty).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by laserlight View Post
    If you can avoid RTTI, avoid it. If you specialise the template, there is no RTTI overhead (though you may add to executable code bloat).
    I was using the template because I need excatly the same thing for almost every case except for a few (int, short). In those cases, 50% of the code's the same and 50% is diff. Does it make more sense to overload the functions where I have one for int vectors, one for short vectors and one with a template for everything else instead of doing a few if...else statements? Why? (And will it even work to have the function overloaded with two specialized versions and one templated version?)

    Thanks for the help!

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, you probably need to write that 50% of different code a few times over either way, right? So by making a specialized template, you can avoid the RTTI (avoid using typeid()) and making the code overly complicated.

    If you have code that is common to all cases, why not make that into a second template function?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I've had some bad experiences with RTTI when using it with derived classes... so I try to avoid it whenever possible.
    I had a case where I needed a template to do one thing for signed numbers and another for unsigned. After some searching I came up with this non-RTTI solution:
    Code:
    	template <typename E,
    		  typename T,
    		  typename A,
    		  typename N>
    	void StringToNum( const std::basic_string<E, T, A>&  str,
    							 N&  num )
    	{
    		if ( std::numeric_limits<N>::is_signed == true )
    		{
    			signed long lnum = 0;
    			StringToNumHelper( str, lnum, num );
    		}
    		else
    		{
    			unsigned long lnum = 0;
    			StringToNumHelper( str, lnum, num );
    		}
    	}
    Most of the time, you'll probably be able to find an equally simple non-RTTI solution if you search hard enough.

  15. #15
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,597
    Once I found out template using functions actually compile on run-time, it scared me away from them for good. (hopefully I'll never NEED them)
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21