I'm trying write a function that is templated on three things:
- First type.
- Second type.
- Function with arguments First and Second type.
The code looks like this:
#include <iostream>
#include <typeinfo>
using namespace std;
// Assume that this function is in a library. Can't be modified.
void bar(int x, int y) {
cout << x << endl;
cout << y << endl;
}
// My code is below:
template <typename Type1, typename Type2, void (*fn)(Type1, Type2)>
void foo(Type1 x1, Type2 x2) {
fn(x1,x2);
}
int main() {
foo<int, int, &bar>(1,2);
}
The code works but I'm unhappy that my template has to include <int, int, &bar>
. I hoped that the compiler would figure out that bar has int, int
as parameters and figure it out.
I tried listing the function first and the types second but in the declaration, Type1
wasn't recognized in the function prototype because it is defined later in the same prototype.
Is there an elegant solution?
Edit: I definitely don't want to pass a pointer to bar
on the stack. I want to be templated on bar
. The params should be just (1, 2)
.
Edit2: And by that, I mean that I want to write foo<&bar>(1,2)
.