M
mathieu
Hi there,
I have the following -ridiculously- simple problem. How do I do a
proper static_cast in the following (pseudo) code.
template <typename T>
T compute_average()
{
const T array[10] = { 0,1,2,3,4,5,6,7,8,9 };
double sum = 0;
for(int i = 0; i < 10; ++i)
sum += array;
sum /= 10;
return static_cast<T>(sum); // BUG: inappropriate in some case.
}
I am thinking of:
template <typename T>
T smart_static_cast(double t)
{
return static_cast<T>(t+0.5);
}
template <>
double smart_static_cast(double t)
{
return static_cast<double>(t);
}
template <>
float smart_static_cast(double t)
{
return static_cast<float>(t);
}
Thanks
I have the following -ridiculously- simple problem. How do I do a
proper static_cast in the following (pseudo) code.
template <typename T>
T compute_average()
{
const T array[10] = { 0,1,2,3,4,5,6,7,8,9 };
double sum = 0;
for(int i = 0; i < 10; ++i)
sum += array;
sum /= 10;
return static_cast<T>(sum); // BUG: inappropriate in some case.
}
I am thinking of:
template <typename T>
T smart_static_cast(double t)
{
return static_cast<T>(t+0.5);
}
template <>
double smart_static_cast(double t)
{
return static_cast<double>(t);
}
template <>
float smart_static_cast(double t)
{
return static_cast<float>(t);
}
Thanks