A
Andre Kempe
hej folks.
i have a heap with fixed size and want to determine the depth of a
element with given index at compile-time. therefore i wrote some
templates. however, when i use template index_properties<unsigned int>,
i get a compiler-error, complaining about the template-recursion of
__index_properties__. when i add a partially specialized template (the
three commented lines) to stop the template-recursion, it works.
does anyone how to make it work without this partial specialization???
my compiler is microsoft visual studio 2005
thanks in advance
andre
======================================
template < unsigned int power , int base >
struct nth_power
{ enum { value = nth_power<power-1,base>::value*base }; };
template < int base >
struct nth_power< 0 , base >
{ enum { value = 1 }; };
template< unsigned int level >
struct level_properties {
enum { entries_in_layer = nth_power<level,2>::value };
enum { number_entries = nth_power<level+1,2>::value-1 };
enum { index_first_element = number_entries - entries_in_layer };
};
template< int index , int level >
struct __index_properties__ {
typedef level_properties<level> this_level;
enum { level = (this_level::number_entries>index)? level :
__index_properties__<index,level+1>::level };
};
// template< int index >
// struct __index_properties__<index,20>
// { enum { level = 20 }; };
template < int index >
struct index_properties
{ enum { level = __index_properties__<index,0>::level }; };
i have a heap with fixed size and want to determine the depth of a
element with given index at compile-time. therefore i wrote some
templates. however, when i use template index_properties<unsigned int>,
i get a compiler-error, complaining about the template-recursion of
__index_properties__. when i add a partially specialized template (the
three commented lines) to stop the template-recursion, it works.
does anyone how to make it work without this partial specialization???
my compiler is microsoft visual studio 2005
thanks in advance
andre
======================================
template < unsigned int power , int base >
struct nth_power
{ enum { value = nth_power<power-1,base>::value*base }; };
template < int base >
struct nth_power< 0 , base >
{ enum { value = 1 }; };
template< unsigned int level >
struct level_properties {
enum { entries_in_layer = nth_power<level,2>::value };
enum { number_entries = nth_power<level+1,2>::value-1 };
enum { index_first_element = number_entries - entries_in_layer };
};
template< int index , int level >
struct __index_properties__ {
typedef level_properties<level> this_level;
enum { level = (this_level::number_entries>index)? level :
__index_properties__<index,level+1>::level };
};
// template< int index >
// struct __index_properties__<index,20>
// { enum { level = 20 }; };
template < int index >
struct index_properties
{ enum { level = __index_properties__<index,0>::level }; };