I need to write a Util function (in my c++cli app) that converts a String to a Double or Float or Int.
template<typename T>
static T MyConvert(String^ str) {
return static_cast<T>(System::Convert::ToDouble(str));
}
Is this safe?
Can it somehow convert 2 to 1.999 and then to 1 if I call MyConvert<int>("2")
?
I was wondering why the Convert class isn't templated in the first place? (That would let me call Convert<T>
instead of Convert.ToDouble()
for all types)
This is C++/Cli so I can use any convert methods in c++ or .net, but I only know Convert.ToDouble()|ToString()|ToInt32())
Thanks