I am trying to write a template function that attempts to convert anything into a long.
.h
class General
{
public:
static void delay( int seconds );
template <typename Generic> static long ToLong( Generic source );
template <typename Generic> static double ToDouble( Generic source );
template <typename Generic> static int ToInt( Generic source );
};
.cpp
void General::delay( int seconds )
{
#ifdef WIN32
Sleep( seconds * 1000 );
#else
sleep ( seconds );
#endif
}
template <typename Generic> long General::ToLong( Generic source )
{
if ( strcmp( typeid( source).name(), "long" ) == 0 )
return source;
long a=0;
istringstream o;
o.str(source);
if ( ! (o>>a ))
throw DHException( "StringUtils", "Could not convert [" + source + "] to a long!");
return a;
}
The problem occurs when I attempt to call it but actually pass it something that is already a long:
long temp=5;
long temp2 = General::ToLong( temp )
"../libs/General.cpp", line 22: Error: Could not find a match for std::basic_istringstream<char, std::char_traits<char>, std::allocator<char>>::str(long).
"Hoff.cpp", line 66: Where: While instantiating "static General::ToLong<long>(long)".
"Hoff.cpp", line 66: Where: Instantiated from non-template code.
How do I detect that it is already a long and simply return itself? (Ignore the part about the DHException for now..)