Practically speaking, you'll probably want to actually define the functions in another file, but once they're defined, you should be able to do this (and make sure to do it in a header file)...
1 2 3 4 5
inlinetemplate <class T> do_something {
...
double d = T (v1, v2) ;
...
}
LOL I was just gonna delete that whole last post, Bazzy, because I finally got overwhelmed with curiosity and it just doesn't work that way. What will work follows...
where in the call to do_something() you switch the first parameter to d1 or d2, depending on which "function" you want to use.
(I used ints instead of vects (whatever they are exactly) just to make life simpler, but the same principle should apply.)
You're extremely correct about function pointers being a more obvious solution, though. For all practical purposes there's no real difference between using them and the solution above except for the word "template."
I tried this way because, I had noticed that the use of pointer to functions was terrible for execution time. So I wanted to know, as it uses templates, if the resulting binary was equivalent to the hard coded version :
works, and the above is essentially the same thing except the type is not int, but rather a function pointer.
Not sure if the above is better than a function pointer. The question is: can the compiler inline the function (print, that is)?
In my original reply, the design paradigm I used is used throughout the STL and in general can benefit from inlining if Fn is a function object whose operator() is inlined in a header file.
Wow. That does work. That's a really cool trick, Alexis. Wow. That's the coolest trick I think I've ever seen.
The binaries aren't exactly equivalent. The executable for your template version is a whole 25 KB larger than the .exe for your plain old cout version. It probably executes just a little bit slower for the extra function call, but preprocesssing and compiling takes away all the template stuff and just leaves the functions that are the result.
Never have used g++. Rarely read any compiler output past the "Done" or "Couldn't Be Done" parts either. Using the word "inline" is supposed to guarantee that a function gets inlined, though, so you really shouldn't need to ask. From what I understand compilers and optimizers often inline functions without being told to as well just because that's their job.