One member lists in C ++

I stumbled upon this answer to a question about the powers of integers in C ++: https://stackoverflow.com/a/464829/

I really like this, but I don’t quite understand why the author explicitly uses one enumeration of elements, unlike some integer type. Can someone explain?

+4
source share
1 answer

AFAIK, this is due to older compilers that do not allow you to define constant compilation element data. With C ++ 11 you could do

template<int X, int P> struct Pow { static constexpr int result = X*Pow<X,P-1>::result; }; template<int X> struct Pow<X,0> { static constexpr int result = 1; }; template<int X> struct Pow<X,1> { static constexpr int result = X; }; int main() { std::cout << "pow(3,7) is " << Pow<3,7>::result << std::endl; return 0; } 
+2
source

All Articles