Partial "function" specialization and performance

Hi,

for one of my projects I am writing a computational intensive code in C++: it's a code for the resolution of some equations using different methods that one can choose at compile time. In order to change the method one has to change the behaviour of the innermost loop of the code, by choosing the right "computational kernel". The idea was to use a template function having an "enum" as template parameter. It's something like:

1
2
3
4
enum method{method1,method2};

template<typename A, method M>
void core(A const & input, A & output);


then the idea was to make various specializations of "core" for various methods and let the compiler choose the right one.

Now the problem is: I need the "core" to have a second template parameter "A" for other reasons and I also know that in C++ one can't do partial function specialization. So I came up with something like:

1
2
3
4
template<typename A, method M>
struct core {
    core(A const & input, A & output);
};


so that "core" can be used like if it was a function returning a void.

But now I am a little bit concerned about the performances: should I expect it to be slower then the "true function" version?

Thanks in advance.
If the decision of the method is taken in compile time, why not use macro definitions?

1
2
3
4
5
6
#ifdef NEWTON_RAPHSON
//Add the Newton-Raphson code here.
#elifdef GAUSSIAN_ELIMINATION
//Add the Gaussian elimination code here.
...
#endif 


Now, all you have to do before compiling is defining any of the macro names and you are done! You can define it on a header or code file, or you can define it via the project properties in some IDE's (like Visual Studio).
You're kidding, right? You are advocating using macros?

There is no computational penalty. Templates are resolved at compile time. There might be a little overhead on the stack, but that is about the same as a function call.
Duoas:

Do you see me giggling like a little girl because you caught me "kidding"?

I am no good at C++ templates so I offered my help with what I know and understand. At no point in my post did I say that templates weren't a good idea. I just helped with an issue with my own way of resolving it based on whatever little knowledge I have, and considering the fact that my solution is indeed simple.

Do you care to pitch in? Go for it. Clearly, templates are your thing.
Thanks to you both for your help.

I am not using macros because I want to provide the user of my library the possibility of plugging in other custom methods, without the need to touch the code inside my main class. Moreover that piece of code needs to be a routine that can be called in different places in the code.

My concern was the overhead of using a class constructor as if it was a function, considering the fact that this is really in the core of the code and that a run is expected to take from a few minutes up to a few hours on my Linux workstation depending on the problem.
Topic archived. No new replies allowed.