A
aaragon
I have this scenario: several arrays for which I have their fixed
values at compilation time. Now, at runtime I need to access a
specific array depending on an integer but I want to avoid if and
switch statements.
My first approach was to rely on partial template specialization.
Therefore, I have:
// .h file
template <int v> SomeClass;
template <> struct SomeClass<1> {
static double loc(unsigned short i) {
return loc_;
}
static const double loc_[1];
};
template <> struct SomeClass<2> {
static double loc(unsigned short i) {
return loc_;
}
static const double loc_[2];
};
// and so on until 30 partial specializations
// .cpp file
const double SomeClass<1>::loc_[] = { 0.4 };
const double SomeClass<2>::loc_[] = { 0.2, 0.6 };
// and so on until 30 partial specializations
so I called the function like this:
double location = SomeClass<3>::loc(2);
Well, this approach worked fine until I changed the code so that the
integer is not known until runtime. So the question is...
Is there a way to do the same at runtime without having a switch or if
statement? Is there a way to do function overloading at runtime? I
thought that I could have something like the following:
struct SomeClass {
int val;
SomeClass(int v) : val(v) {}
double loc(unsigned short i) {
return locImpl(classB(val),i);
}
double locImpl(classB(1), int i) {
const double SomeClass<1>::loc_[] = { 0.4};
return loc_;
}
double locImpl(classB(2), int i) {
const double SomeClass<2>::loc_[] = { 0.2, 0.6 };
return loc_;
}
};
and convert the integer to some class and rely on function
overloading, but I couldn't find a way to do this. Any ideas? Thank
you,
a²
values at compilation time. Now, at runtime I need to access a
specific array depending on an integer but I want to avoid if and
switch statements.
My first approach was to rely on partial template specialization.
Therefore, I have:
// .h file
template <int v> SomeClass;
template <> struct SomeClass<1> {
static double loc(unsigned short i) {
return loc_;
}
static const double loc_[1];
};
template <> struct SomeClass<2> {
static double loc(unsigned short i) {
return loc_;
}
static const double loc_[2];
};
// and so on until 30 partial specializations
// .cpp file
const double SomeClass<1>::loc_[] = { 0.4 };
const double SomeClass<2>::loc_[] = { 0.2, 0.6 };
// and so on until 30 partial specializations
so I called the function like this:
double location = SomeClass<3>::loc(2);
Well, this approach worked fine until I changed the code so that the
integer is not known until runtime. So the question is...
Is there a way to do the same at runtime without having a switch or if
statement? Is there a way to do function overloading at runtime? I
thought that I could have something like the following:
struct SomeClass {
int val;
SomeClass(int v) : val(v) {}
double loc(unsigned short i) {
return locImpl(classB(val),i);
}
double locImpl(classB(1), int i) {
const double SomeClass<1>::loc_[] = { 0.4};
return loc_;
}
double locImpl(classB(2), int i) {
const double SomeClass<2>::loc_[] = { 0.2, 0.6 };
return loc_;
}
};
and convert the integer to some class and rely on function
overloading, but I couldn't find a way to do this. Any ideas? Thank
you,
a²