Keith H Duggar wrote:
>> > I suspect your "integer pow" just iterates to do the exponentiation.
>>
>> No, I actually used compile time recursion.
>
> Really? How interesting, can you please post the code for
> the function?
>
> Keith
This is based on ideas from _C++_Templates_The_Complet_Guide_
http://www.josuttis.com/tmplbook/
I really don't know if this has any advantages, nor do I know if it is even
correct. I ran it through a few simple tests, but haven't revisited it
since. As I am working on my testing infrastructure.
/************************************************** *************************
* Copyright (C) 2004 by Steven T. Hatton *
*
*
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
************************************************** *************************/
#ifndef STH_TMATHPOWER_H
#define STH_TMATHPOWER_H
#include<stdexcept>
namespace sth {
namespace tmath {
/**
@author Steven T. Hatton
*/
template <size_t Exponent_S, typename T>
class PowerOf
{
public:
static T eval(const T& base)
{
return base * PowerOf<Exponent_S - 1, T>::eval(base);
}
};
template <typename T>
class PowerOf<1, T>
{
public:
static T eval(const T& base)
{
return base;
}
};
template <typename T>
class PowerOf<0, T>
{
public:
static T eval(const T& base)
{
if(!base)
{
throw std::logic_error(
"sth::tmath:

owerOf<0>(0) is an indeterminate form.");
}
return 1;
}
};
template <size_t Exponent_S, typename T>
T power(const T& base)
{
return PowerOf<Exponent_S, T>::eval(base);
}
};
}
#endif
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell