Or you could use a litte bit of template metaprogramming :)
template
struct Pow
{
enum { result = X*Pow::result };
};
template
struct Pow
{
enum { result = 1 };
};
template
struct Pow
{
enum { result = X };
};
int main()
{
std::cout << "pow(3,7) is " << Pow<3,7>::result << std::endl;
return 0;
}
This code has the best complexity, O(1), because the evaluation will happen at compile time. Of course this will only work with integer values.
However, this function is is only provided for completeness (and fun).