I am trying to write a template to calculate the power of a number during compile time (I am not a template meta-programming expert so any comment is appreciated). Below is the code:
template<typename T, T X, uint64_t P>
struct Pow
{
static constexpr T result = X * Pow<T,X, P - 1>::result;
};
template<typename T, T X>
struct Pow<T, X, 0>
{
static constexpr T result = 1;
};
template<typename T, T X>
struct Pow<T, X, 1>
{
static constexpr T result = X;
};
which I need to call like:
Pow<decltype(4), 4, 2>::result
Question: Is there any way in writing a helper template so that the call skips the decltype
? For example:
Pow<4, 2>::result
I have read the following but so far I couldn't see an answer (it seems quite the opposite) this, this, and this.
Starting from C++17, you can use an auto
type for the X
template value
template <auto X, int64_t P>
struct Pow
{
static constexpr decltype(X) result = X * Pow<X, P - 1>::result;
};
template <auto X>
struct Pow<X, 0>
{
static constexpr decltype(X) result = 1;
};
And you can also see that, given the 0
partial specialization, the 1
partial specialization is superfluous (also C++11/C++14).
Before C++17... the best I can imagine, to avoid to explicit the T
type, pass through a macro definition (that usually is heavily discouraged but, in this case, I suppose can be reasonable).
Something as
#define PowMacro(X, P) Pow<decltype(X), X, P>
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With