Thread: In template function, how query the type?

  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
    28,413
    One option is to specialise the template.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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
    28,413
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    2,158
    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)

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, 05:45 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM