Simple static_cast issue

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
 
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 think I'll settle for this one, which should be nicely inline'd.

template <typename T>
struct helper
{
static const double offset = 0.5;
};
template <>
struct helper<float>
{
static const double offset = 0.;
};
template <>
struct helper<double>
{
static const double offset = 0.;
};


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 + helper<T>::eek:ffset);
}
 
N

Niels Dekker - no return address

Mathieu said:
I think I'll settle for this one, which should be nicely inline'd.

template <typename T>
struct helper
{
static const double offset = 0.5;
};

I'm sorry, C++ doesn't allow an in-class initializer for a static
floating point member. As I tested on www.comeaucomputing.com/tryitout/

"ComeauTest.c", line 4: error: a member of type "const double" cannot
have an in-class initializer
static const double offset = 0.5;
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 + helper<T>::eek:ffset);
}


The next version of C++ will have an std::round function :) But
anyway, Fernando Cacciola's numeric conversion library might also be of
help to you: www.boost.org/libs/numeric/conversion

Kind regards, Niels
 
G

Greg Herlihy

  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.

}


What determines whether the static_cast<> is inappropriate? If the
cast is not appropriate whenever a double cannot be converted to a
type T, then the function template could verify that double converts
to type T, and take the appropriate action otherwise. (The example
below uses a static_assert for the purpose of illustration.)

#include <tr1/type_traits>

using std::tr1::is_convertible;

template <typename T>
T compute_average()
{
static_assert( is_convertible<double, T>::value,
"Error - type used to instatiated
compute_average()"
" is not convertible to a double" );

// or use BOOST_STATIC_ASSERT in place of static_assert

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.0;
return T(sum); // BUG: inappropriate in some case.
}

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top