Thread: In template function, how query the type?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    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!

  7. #7
    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.

  8. #8
    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.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by cpjust View Post
    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.
    Overloading would still be better here, I think. For example, like this:
    Code:
    #include <boost/mpl/bool.hpp>
    
    	template <typename E,
    		  typename T,
    		  typename A,
    		  typename N>
    	void StringToNumHelper( const std::basic_string<E, T, A>&  str,
    							 N&  num, boost::bool_<true> )
    	{
    		// Signed implementation.
    	}
    
    	template <typename E,
    		  typename T,
    		  typename A,
    		  typename N>
    	void StringToNumHelper( const std::basic_string<E, T, A>&  str,
    							 N&  num, boost::bool_<false> )
    	{
    		// Unsigned implementation.
    	}
    
    	template <typename E,
    		  typename T,
    		  typename A,
    		  typename N>
    	void StringToNum( const std::basic_string<E, T, A>&  str,
    							 N&  num )
    	{
    		StringToNumHelper( str, num,
    			boost::bool_<std::numeric_limits<N>::is_signed>() );
    	}
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by 6tr6tr View Post
    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?)
    Just because you can't see the alternate solution doesn't mean it isn't there. If 50% of the code is the same, then that 50% should be broken out into another function, so that only the changing 50% is written in the specializations.

    RTTI is a crutch. It often indicates poor programming.

  11. #11
    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?

  12. #12
    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.

  13. #13
    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.

  14. #14
    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

  15. #15
    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.

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