#include <sstream>
template< typename Output, typename Input >
Output custom_cast( const Input &In )
{
std::stringstream Converter; //关键核心使用字符流
Converter << In; //写入数据
Output Out;
Converter >> Out; //输出数据
return Out;
}
int main()
{
float a = custom_cast< float >( "123" );
double b = custom_cast< int >( 123 );
std::string c = custom_cast< std::string >( 123.0 );
int d = custom_cast< int >( "123" );
return 0;
}
boost::lexical_cast简单实现
最新推荐文章于 2024-08-14 19:43:36 发布