so i have got function made by template. And i want to use atoi() for integer ftoi() for float and so on. But i don't know how to recognize what type is being used right now and how to make this conditional statement for precompiling.
Posted on 2005-05-29 06:58:19 by AceEmbler
I assume you mean to convert a string to an int or float or whatever? You could use a stringstream to extract the value; that is, create the stringstream containing the string then use the >> operator with your type:


#include <sstream>

template<typename _T>
_T myfunction(const string& str)
{
_T retval;
stringstream ss(str);
ss >> retval;
return retval;
}

Posted on 2005-05-29 08:54:05 by stormix
Probably this is not what i want to do.

Look at this pseudo code.

MyFunc(Type Var)
{
  if (Type==float) ftoa(Buffer,Var);

  if(Type==int) itoa(Buffer,Var);
}
Posted on 2005-05-29 10:16:38 by AceEmbler

In that case do the opposite


#include <sstream>

template<typename _T>
string myfunction(_T val)
{
stringstream ss;
ss << val;
return ss.str();
}


or use overloading and create a version for each type.
Posted on 2005-05-29 10:48:34 by stormix
Thx for your answers i will try the stringstream.

====================================
i tried to Spell Check and i have got the following error:
strpos(): Offset not contained in string.
====================================

"This is interesting the Spell Check tried to change 'Thx' to 'THC'  :lol:"
Posted on 2005-05-29 11:27:03 by AceEmbler
stringstreams are safe and neat, but they do have a fair amount of overhead; if this is not acceptable to you, have a look at "specialization". It only allows entire functions to be specialized though, so you might have some code duplication to do... and perhaps normal function overloading is sufficient.
Posted on 2005-05-29 19:52:34 by f0dder