Keith H Duggar wrote:
> Really? How interesting, can you please post the code for
> the function?
>
> Keith
This is based on ideas from _C++_Templates_ The_Complet_Gui de_
http://www.josuttis.com/tmplbook/
...
Not to quibble, but this isn't really a "function" is? It is
a class implementation that happens to have a function you
call. That's why I asked you to post the code because had you
built a compile time recursive function that would have been
most unique.
I never said it was a function. But I believe what you end up with is
pretty much a function built at compile-time using recursion. It only uses
static members, and I suspect it's pretty much loaded and executed in once
chunk. I haven't read that discussion yet.
In this version I basically used a binary tree, but reused the calculations
which would have been identical. If you compile and run the program, you
will have done as much testing as I have.
/*************** *************** *************** *************** ***************
* Copyright (C) 2004 by Steven T. Hatton *
*
ha*****@globals ymmetry.com *
* *
* 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. *
*************** *************** *************** *************** ***************/
#include <iostream>
#include <iomanip>
#include <stdexcept>
using std::cout;
using std::logic_erro r;
template<size_t S>
struct Pwr{
inline static size_t pwr(const size_t& b)
{
size_t p = Pwr<S/2>::pwr(b);
return (S & 1) ? p * p * b : p * p;
}
};
template<>
struct Pwr<1>{
inline static size_t pwr(const size_t& b) { return b; }
};
// NOTE: I'm not handling Pwr<0>, but it's trivial.
template<size_t S>
struct Powers{
inline static size_t power(const size_t& b)
{
cout << b << "^" << S << " = " << std::setw(8) << Pwr<S>::pwr(b) << "
";
return Powers<S-1>::power(b);
}
};
template<>
struct Powers<0>{
inline static size_t power(const size_t& b)
{
if(!b){ throw logic_error("ER ROR: indeterminate form 0^0 "); }
cout << "\n";
return 0;
}
};
int main()
{
for(size_t s = 1; s < 10; s++)
{
Powers<5>::powe r(s);
}
return 0;
}
--
"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