Is this casting safe?

Posted by Itsik on Stack Overflow See other posts from Stack Overflow or by Itsik
Published on 2010-06-03T14:12:29Z Indexed on 2010/06/03 14:14 UTC
Read the original article Hit count: 276

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about c++

Related posts about casting