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.
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;
}
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);
}
Look at this pseudo code.
MyFunc(Type Var)
{
if (Type==float) ftoa(Buffer,Var);
if(Type==int) itoa(Buffer,Var);
}
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.
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:"
====================================
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:"
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.