【发布时间】:2013-11-09 05:19:05
【问题描述】:
考虑以下计算整数的整数幂的元函数:
template <std::intmax_t Base, std::intmax_t Exponent>
struct integer_power
{
static constexpr std::intmax_t temporary = integer_power<Base, Exponent/2>::value;
static constexpr std::intmax_t value = temporary*temporary*(Exponent%2 == 1 ? Base : 1);
static constexpr bool overflow = /* something */;
};
template <std::intmax_t Base>
struct integer_power<Base, 0>
{
static constexpr std::intmax_t value = 1;
static constexpr bool overflow = false;
};
当结果不能存储在整数中时,我希望内部变量溢出为真。该怎么做?
【问题讨论】:
标签: c++ integer metaprogramming template-meta-programming integer-overflow