8

考虑以下代码:

template <std::intmax_t Base, std::intmax_t Exponent> 
struct integer_power_bounded
{
    static_assert(Exponent >= 0, 
                  "Error in 'integer_power_bounded': 'Exponent >= 0' is false");
    static constexpr std::intmax_t value = /* something */;
};

template <std::intmax_t Base> 
struct integer_power_bounded<Base, 0>
{
    static constexpr std::intmax_t value = 1;
};

而不是/* something */,我想返回std::numeric_limits<std::intmax_t>::min()或者std::numeric_limits<std::intmax_t>::max()如果Base^Exponent不能用std::intmax_t. 困难的是在计算过程中避免溢出,因为它们在编译时会产生错误。

怎么做(没有提升)?

4

1 回答 1

19

基于 SFINAE 的版本:

#include <cstdint>
#include <cmath>
#include <limits>
#include <type_traits>

constexpr std::intmax_t integer_power(std::intmax_t base,
                                      std::intmax_t exponent)
{
    return (exponent == 0) ? 1 :
           (exponent % 2 == 0) ?  integer_power(base, exponent/2)
                                 *integer_power(base, exponent/2) :
           base*integer_power(base, exponent-1);
}

namespace detail
{
    template<std::intmax_t base, std::intmax_t exponent,
             std::intmax_t res = integer_power(base,exponent)>
    constexpr std::intmax_t pow_helper(int)
    {
        return res;
    }

    template<std::intmax_t base, std::intmax_t exponent>
    constexpr std::intmax_t pow_helper(...)
    {
        return (exponent%2 == 0 || base > 0)
               ? std::numeric_limits<std::intmax_t>::max()
               : std::numeric_limits<std::intmax_t>::min();
    }
}

template<std::intmax_t base, std::intmax_t exponent>
constexpr std::intmax_t integer_power_bounded()
{
    return detail::pow_helper<base,exponent>(0);
}

使用示例:

#include <iostream>
int main()
{
    std::cout << sizeof(std::intmax_t) << '\n';

    constexpr auto p2t6 = integer_power_bounded<2, 6>();
    constexpr auto p2t62 = integer_power_bounded<2, 62>();
    constexpr auto p2t63 = integer_power_bounded<2, 63>();
    constexpr auto p2t64 = integer_power_bounded<2, 64>();
    constexpr auto p2t65 = integer_power_bounded<2, 65>();

    std::cout << "2^6 == " << p2t6 << '\n';
    std::cout << "2^62 == " << p2t62 << '\n';
    std::cout << "2^63 == " << p2t63 << '\n';
    std::cout << "2^64 == " << p2t64 << '\n';
    std::cout << "2^65 == " << p2t65 << '\n';

    constexpr auto pm2t6 = integer_power_bounded<-2, 6>();
    constexpr auto pm2t62 = integer_power_bounded<-2, 62>();
    constexpr auto pm2t63 = integer_power_bounded<-2, 63>();
    constexpr auto pm2t64 = integer_power_bounded<-2, 64>();
    constexpr auto pm2t65 = integer_power_bounded<-2, 65>();

    std::cout << "-2^6 == " << pm2t6 << '\n';
    std::cout << "-2^62 == " << pm2t62 << '\n';
    std::cout << "-2^63 == " << pm2t63 << '\n';
    std::cout << "-2^64 == " << pm2t64 << '\n';
    std::cout << "-2^65 == " << pm2t65 << '\n';
}

输出:

8
2^6 == 64
2^62 == 4611686018427387904
2^63 == 9223372036854775807
2^64 == 9223372036854775807
2^65 == 9223372036854775807
-2^6 == 64
-2^62 == 4611686018427387904
-2^63 == -9223372036854775808
-2^64 == 9223372036854775807
-2^65 == -9223372036854775808

解释:

常量表达式可能不包含未定义行为 [expr.const]/2:

  • 具有未定义行为的操作 [注意:包括,例如,有符号整数溢出、某些指针算术、除以零或某些移位操作 -结束注释];

因此,每当unbounded integer_power产生溢出时,用于声明的表达式std::integral_constant都不是有效的常量表达式;替换失败并使用回退功能。

于 2013-11-09T14:22:46.033 回答