Originally Posted by
cpjust
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>() );
}