I have a set of functions that are templated by both an integer type Indexand a class type T, which I "partially specialize" as follows:
enum Index{One,Two,Three,Four};
template<int I>
struct Foo{
template<typename T> static void bar(const T& x){ std::cout <<"default" << endl; }
};
template<>
struct Foo<One>{
template<typename T> static void bar(const T& x){ std::cout << "one" << endl; }
};
I use this to select a specific index at run time using the switch statement (which should lead to an efficient lookup table). The switch is independent of T:
template<typename T>
void barSwitch(int k, const T& x){
switch(k){
case ONE: Foo<ONE>::bar(x); break;
case TWO: Foo<TWO>::bar(x); break;
case THREE: Foo<THREE>::bar(x); break;
}
}
, , Foo , . , , . " " barSwitch "Foo", . , , - :
#define createBarSwitch(f,b) \
template<typename T> \
void barSwitch(int k, const T& x){ \
switch(k){ \
case ONE: f<ONE>::b(x); break; \
case TWO: f<TWO>::b(x); break; \
case THREE: f<THREE>::b(x); break; \
}\
}
- , ++?