On 07/28/10 10:53, Hite, Christopher wrote:
[snip]
struct DecodeContext;
struct M1{
static const int id=1;
static void decode(DecodeContext& ){}
};
struct M2{
static const int id=2;
static void decode(DecodeContext& ){}
};
struct M3{
static const int id=3;
static void decode(DecodeContext& ){}
};
template <int> struct ListMember;
template <> struct ListMember<1>{ typedef M1 type; };
template <> struct ListMember<2>{ typedef M2 type; };
template <> struct ListMember<3>{ typedef M3 type; };
template<int i>
void foo(int id, DecodeContext& dc) {
typedef typename ListMember<i>::type M;
if(M::id==id)
M::decode(dc);
else
foo<i-1>(id,dc);
}
template<>
void foo<0>(int id, DecodeContext& dc) {}
int main(){
DecodeContext& dc= *(DecodeContext*)0;// junk for now
int id=2; //sometime runtime dependent
foo<3>(id,dc);
return 0;
}
[snip]
Wouldn't the following simplification work just as well?
template <int Id> struct ListMember{
static void decode(DecodeContext& ){}
};
template<int i>
void foo(int id, DecodeContext& dc) {
if(i=id)
ListMember<i>::decode(dc);
else
foo<i-1>(id,dc);
}