Re: looping compiler
In article
<390c98b1-3e27-48ba-8394-67f55fae62f7@d45g2000hsc.googlegroups.com>,
<moje_spamerskie_konto@yahoo.com> wrote:
I have a small piece of code and I can`t figure out why my compiler
loops while compiling it. Here is a source:
...
long long const my_max_int = 1000;
template <int radix, int k>
struct power {
static const long long result = power<radix, k-1>::result * (long
long) radix;
};
template <int radix>
struct power<radix, 0> {
static const long long result = 1;
};
template <int radix, int k>
struct param_max_repr_size {
enum { result = ((power<radix, k>::result < my_max_int) ?
(int) param_max_repr_size<radix, k+1>::result : k) };
};
int main() {
int n;
n = param_max_repr_size<100, 1>::result;
}
In this example I want to count the smallest 'k' such as 100^k >
my_max_int.
Any clue?
mati
First power is O(N) in classes created, power can be written in
70 lines that is O(log2(N) ) in classes created, in particular a 64 bit
long long creates about 128 classes not 2^64*k where k>=1.
something like the following is the 'power algorithm that is O(log2(N))
in class defintions from the expanding templates:
as the #if 1 ... #endif is a test that it works. No other headers
required.
<code>
template <long long N>
struct LongLong
{
static const long long value = N;
};
template <bool B,class T,class F>
struct select
{
typedef T type;
};
template <class T,class F>
struct select<false,T,F>
{
typedef F type;
};
template <int N>
struct is_odd
{
static const bool value = N & 1;
};
template <long long R,long long B,bool Test>
struct power_aux_1
{
static const long long value = R*B;
};
template <long long R,long long B>
struct power_aux_1<R,B,false>
{
static const long long value = R;
};
template <long long B,int N,long long R>
struct power_aux:select
<
N==0,
LongLong<R>,
power_aux
<
B*B,
N >> 1,
power_aux_1
<
R,
B,
is_odd<N>::value
>::value
>
::type
{
};
template <long long B,int N>
struct power:power_aux<B,N,1LL>
{
};
#if 1
template <bool B> struct compiler_error{};
template <> struct compiler_error<true> {typedef int type;};
typedef select
<
power<2,5>::value == 32,
compiler_error<true>,
compiler_error<false>
::type::type foo;
#endif
</code>
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]