J
J Anderson
Hi There,
I have a lot of code that looks like this:
//(The example below is not real.)
void func1(...)
{
for (...)
{
for (int n=0; n<size; n++)
{
if (A[B->m_a]->method3(C[D->m_b[n]]))
{
...
}
}
}
}
void func2(...)
{
for (...)
{
for (int n=0; n<size; n++)
{
//D and B are swapped and that is the only difference
if (A[D->m_b[n]]->method3(C[B->m_a]))
{
...
}
}
}
}
The code in the … is the same in all methods. I have about 4 or 5 of
these methods where the format is exactly the same, but a few parameters
are swapped around. For maintenance reasons I’d like to make them into
one function using templates (or something), however I don’t want to
sacrifice efficiency. I tried the following, which I didn’t think would
work (and doesn’t), because (as far as I know) you can't pass objects
other then constant integers vir templates..
template <class X, class Y>
void funcX(...)
{
for (...)
{
for (int n=0; n<size; n++)
{
if (A[X]->method2(C[Y]))
{
...
}
}
}
}
A call would be like:
funcX<D->m_b[n], B->m_a>(...);
or
funcX<B->m_a, D->m_b[n]>(...);
I know I could do it with # macros, but that seems messy to me (unless
you can show me something neat). I don’t really have a choice about
changing the rest of the design.
Any help would be greatly appreciated.
Thanks
I have a lot of code that looks like this:
//(The example below is not real.)
void func1(...)
{
for (...)
{
for (int n=0; n<size; n++)
{
if (A[B->m_a]->method3(C[D->m_b[n]]))
{
...
}
}
}
}
void func2(...)
{
for (...)
{
for (int n=0; n<size; n++)
{
//D and B are swapped and that is the only difference
if (A[D->m_b[n]]->method3(C[B->m_a]))
{
...
}
}
}
}
The code in the … is the same in all methods. I have about 4 or 5 of
these methods where the format is exactly the same, but a few parameters
are swapped around. For maintenance reasons I’d like to make them into
one function using templates (or something), however I don’t want to
sacrifice efficiency. I tried the following, which I didn’t think would
work (and doesn’t), because (as far as I know) you can't pass objects
other then constant integers vir templates..
template <class X, class Y>
void funcX(...)
{
for (...)
{
for (int n=0; n<size; n++)
{
if (A[X]->method2(C[Y]))
{
...
}
}
}
}
A call would be like:
funcX<D->m_b[n], B->m_a>(...);
or
funcX<B->m_a, D->m_b[n]>(...);
I know I could do it with # macros, but that seems messy to me (unless
you can show me something neat). I don’t really have a choice about
changing the rest of the design.
Any help would be greatly appreciated.
Thanks