Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > compile time va...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 4 Topic 45824 of 45877
Post > Topic >>

compile time value -> index calculation

by Christof Warlich <cwarlich@[EMAIL PROTECTED] > May 9, 2008 at 01:29 PM

Hi all,

in the following example, Index<unsigned int x>::value allows to calculate
the "rounded up" index 
from any "increasing" value during compile time. Unfortunately, the
definition of the index - value 
pairs does not really look nice.

The definition through an array would look much nicer, but does not work,
see below.

As the definition of the index - value pairs are supposed to become part
of the API, does anyone 
have an idea how this could be beautified?

Thanks,

Christof

#include <iostream>
using namespace std;

// definition of the index - value pairs
template<unsigned int index> struct Value {};
template<> struct Value<0> {static const unsigned int value = 1;};
template<> struct Value<1> {static const unsigned int value = 10;};
template<> struct Value<2> {static const unsigned int value = 100;};
template<> struct Value<3> {static const unsigned int value = 1000;};
template<> struct Value<4> {static const unsigned int value = 10000;};

// much more readable than above, but does not work, see below
const unsigned int a[] = {1, 10, 100, 1000, 10000};

template<unsigned int size, unsigned int index = 0> struct Index {
     static const unsigned int value = (size <= Value<index>::value ?
index : Index<size, index + 
1>::value);
     // the following does not work, as even const arrays are not allowed
to be used in const 
expressions?!
     //static const unsigned int value = (size <= a[index] ? index :
Index<size, index + 1>::value);
};
// needed to terminate recursion, would be nice if it could be calculate
from the index - value 
definitions from above
template<unsigned int size> struct Index<size, 5> {
     static const unsigned int value = 0xffffffff;
};

int main(void) {
     cout << Value<0>::value << " " << Value<  3>::value << " " << Value< 
 4>::value << endl;
     cout << Index<0>::value << " " << Index<100>::value << " " <<
Index<6000>::value << endl;
     //cout << Value<8>::value << endl; // gives a compiler error as
intended
     cout << hex << Index<10001>::value << endl; // would be nice if this
gives a compiler error too
}




 4 Posts in Topic:
compile time value -> index calculation
Christof Warlich <cwar  2008-05-09 13:29:50 
Re: compile time value -> index calculation
gnuyuva <gnuyuva@[EMAI  2008-05-09 05:07:59 
Re: compile time value -> index calculation
Christof Warlich <cwar  2008-05-09 15:06:39 
Re: compile time value -> index calculation
gnuyuva <gnuyuva@[EMAI  2008-05-10 01:33:28 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon May 12 7:01:17 CDT 2008.