I have a class that takes a few template parameters:
template<typename A, typename B, typename C,typename D>
class MyClass {
// ...
};
The types A,B,C and D are selected from a user specified input
file (properties file):
A = 1
B = 2
C = 1
D = 3
I then parse this file an need to create MyClass with the correct
types:
void createMyClass (int a, int b, int c, int d) {
switch ( a ) {
case 1 :
typedef test::Green ColorType;
switch ( b ) {
case 1 :
typedef test::Water MediumType;
switch ( c ) {
case 1 :
typedef test::Linear InterpolationType;
MyClass<ColorTyper, MediumType, InterpolationType
myClass;
break;
case 2 :
typedef test::Cubic InterpolationType;
MyClass<ColorTyper, MediumType, InterpolationType
myClass;
break;
default :
}
break;
case 2 :
// ....
break;
default :
}
break;
case 2 :
typedef test::Blue ColorType;
switch ( b ) {
case 1 :
typedef test::Water MediumType;
switch ( c ) {
case 1 :
typedef test::Linear InterpolationType;
MyClass<ColorTyper, MediumType, InterpolationType
myClass;
break;
case 2 :
typedef test::Cubic InterpolationType;
MyClass<ColorTyper, MediumType, InterpolationType
myClass;
break;
default :
}
break;
case 2 :
break;
default :
}
break;
default :
}
}
But this switch grows extremely large when the number of choices
for each type grows and is also very ugly/error prone. It could be
nice if it was possible to do something like this instead:
T getAType(int a) {
switch ( a ) {
case 1 :
typedef test::Green ColorType;
break;
case 2 :
typedef test::Blue ColorType;
break;
default :
}
return ColorType;
}
T getBType(int b) {
switch ( b ) {
case 1 :
typedef test::Water MediumType;
break;
case 2 :
typedef test::Rock MediumType;
break;
default :
}
return MediumType;
}
int main(){
typedef getAType(1) ColorType;
typedef getBType(2) MediumType;
...
MyClass<ColorType, MediumType, ...>
}
But this is not supported in C++. Any ideas on how to solve this
combinatorics problem?
less ugly. Personally I would just stick with the switch and
perhaps use macros. You have hit the age old compiler vs runtime