template<typename T>
T to(int n)
{
return (T)n;
}
double d = to<double>(3);
float f = to<float>(2);
还可以这样用,挺有意思的
本文介绍了一个使用C++模板函数进行类型转换的例子。通过定义通用模板函数to(), 实现了整数到浮点数类型的转换。该函数可以接受任意整型参数,并返回指定类型的浮点数值。
template<typename T>
T to(int n)
{
return (T)n;
}
double d = to<double>(3);
float f = to<float>(2);
还可以这样用,挺有意思的
315
3231

被折叠的 条评论
为什么被折叠?