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; Once I found out template using function actually compile on run-time, it scared me away from them for good. Eh, ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,442
    Once I found out template using function actually compile on run-time, it scared me away from them for good.
    Eh, function templates are compiled at compile time.
    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

  2. #17
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,597
    Hmm, I was about to point you to that page that said that, but after reading it again, I realized it didn't!
    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

  3. #18
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    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.

  4. #19
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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

  5. #20
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by CornedBee View Post
    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>() );
    	}
    Actually, the StringToNumHelper() implementation is exactly the same for signed & unsigned. The only difference is the signedness of the N type; and since I don't like to copy and paste code for no reason, I did it like that.
    I also could have overloaded StringToNum() for each signed & unsigned integer type, but I just felt like condensing it into 1 function.

  6. #21
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by cpjust View Post
    Actually, the StringToNumHelper() implementation is exactly the same for signed & unsigned. The only difference is the signedness of the N type; and since I don't like to copy and paste code for no reason, I did it like that.
    I also could have overloaded StringToNum() for each signed & unsigned integer type, but I just felt like condensing it into 1 function.
    I understand the value of doing it yourself, but wouldn't boost::lexical_cast<> serve the purpose you're trying for here?

  7. #22
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    If the implementation is the same, why have two functions at all?
    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

  8. #23
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by brewbuck View Post
    I understand the value of doing it yourself, but wouldn't boost::lexical_cast<> serve the purpose you're trying for here?
    Maybe... if I ever get the time to learn about all those cool Boost libraries.

    Quote Originally Posted by CornedBee View Post
    If the implementation is the same, why have two functions at all?
    Because I need to pass either a long or unsigned long to the helper function to do it's job correctly here's the helper function:
    Code:
    	template <typename E,
    		  typename T,
    		  typename A,
    		  typename LN,	// long or unsigned long.
    		  typename N>
    	void
    	StringToNumHelper( const std::basic_string<E, T, A>&  str,
    							 LN&  lnum,
    							  N&  num )
    	{
    		std::basic_stringstream<E, T, A> ss( str );
    
    		if ( !(ss >> lnum) || (lnum < std::numeric_limits<N>::min()) || (lnum > std::numeric_limits<N>::max()) )
    		{
    			throw std::invalid_argument( "Error converting string to number!" );
    		}
    
    		num = static_cast<N>( lnum );
    	}
    I was getting compile errors for everything I did if I tried to combine them into one function.

  9. #24
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    i was in a similar position once.

    the solution i used was to create different derived classes for each type with virtual getters and setters.

  10. #25
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I get it - you need a larger type to avoid overflow.

    Not hard to solve with a simple metafunction, though. Boost might even have such a thing. But then, Boost also has lexical_cast.
    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

Page 2 of 2 FirstFirst 12
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