Assume I have a function like this:
static const boost::int32_t SOME_CONST_VALUE = 1073741823;
template<typename targetType, typename sourceType>
targetType Convert(sourceType source)
{
typedef decltype(source * SOME_CONST_VALUE) MulType_t;
//typedef boost::int64_t MulType_t;
MulType_t val = (MulType_t)source * (MulType_t)SOME_CONST_VALUE;
return val / (MulType_t)SOME_CONST_VALUE;
}
When I call this function like this
boost::int32_t i = std::numeric_limits<boost::int32_t>::max();
boost::int32_t k = Convert<boost::int32_t>(i);
k equals 1, because of the overflow during the multiplication. Casting everything to boost::int64_t will lead to the result I want. But I don’t want to cast a short or char to a int64 value.
So can I use the decltype to get the next larger type of the expression.
You have to make your own specialization of a template for that:
You can also make a macro if you don’t like typing alot:
and then use it like