C
Christof Warlich
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
}
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
}