基于 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
都不是有效的常量表达式;替换失败并使用回退功能。