Sure you can skip the decltype, and you need no structures when using C++ 11 contexpr. For example:
#include <iostream>
#include <type_traits>
template<typename T, class = typename std::enable_if< std::is_arithmetic<T>::value >::type >
constexpr T pow(T n, T power) noexcept {
return power == 1 ? n : n * pow(n,power - 1);
}
int main(int argc, const char* argv) {
static_assert( 4 == pow(2,2) ,"wrong pow");
static_assert( 8.0F == pow(2.0F,3.0F) ,"wrong pow");
static_assert( 256.0 == pow(2.0,8.0) ,"wrong pow");
std::cout << "integer 2^2=" << pow(2, 2) << std::endl;
std::cout << "float 2^3=" << pow(2.0F, 3.0F) << std::endl;
std::cout << "double 2^8=" << pow(2.0, 8.0) << std::endl;
return 0;
}
P.S.
Faster way for racing number in a power. Real code should use something like that since compilation time also does matters.
#include <iostream>
#include <type_traits>
// https://en.wikipedia.org/wiki/Exponentiation_by_squaring
template<typename T>
constexpr T pow(const T base,const T power, typename std::enable_if< std::is_integral<T>::value >::type* = 0) {
return 1 == power
? base
: 0 == power
? 1
: (1 == (power & 1) )
? base * pow(base, power - 1)
: pow(base, (power >> 1) ) * pow( base, (power >> 1) );
}
#ifdef __GNUG__
// GCC able to use most of <cmath> at compile time, check <cmath> header
inline constexpr float pow(float base, float power) noexcept {
return __builtin_powf(base, power);
}
inline constexpr double pow(double base, double power) noexcept {
return __builtin_pow(base, power);
}
inline constexpr long double pow(long double base,long double power) noexcept {
return __builtin_powl(base, power);
}
#else
// slow
template<typename T>
constexpr T pow(T base, T power, typename std::enable_if< std::is_floating_point<T>::value >::type* = 0) noexcept {
return power == 1.0 ? base : base * pow(base,power - static_cast<T>(1.0) );
}
#endif // __GNUG__
int main(int argc, const char** argv) {
static_assert( 4 == pow(2,2) ,"wrong pow");
static_assert( 1024 == pow(2L,10L) ,"wrong pow");
static_assert( (1 << 20) == pow(2LL,20LL) ,"wrong pow");
std::cout << "integer 2^1=" << pow(2, 1) << std::endl;
std::cout << "integer 2^2=" << pow(2, 2) << std::endl;
std::cout << "long 2^10=" << pow(2L, 10L) << std::endl;
std::cout << "long long 2^20=" << pow(2LL, 20LL) << std::endl;
static_assert( 8.0F == pow(2.0F,3.0F) ,"wrong pow");
static_assert( 256.0 == pow(2.0,8.0) ,"wrong pow");
static_assert( 1024.0L == pow(2.0L,10.0L) ,"wrong pow");
std::cout << "float 2^3=" << pow(2.0F, 3.0F) << std::endl;
std::cout << "double 2^8=" << pow(2.0, 8.0) << std::endl;
std::cout << "long double 2^10=" << pow(2.0L, 10.0L) << std::endl;
return 0;
}
constexpr
function work equally well, in your case? – Fanlightconstexpr
function that returnsPow<T, x, y>::result
; which will be logically equivalent, at compile time, to what you have here. – FanlightPow<double, 2.0, -1>
. – Exclaim