Function with templates as template argument
How do I pass a function that has a template argument to another function?
template <class _T>
void inc(int &x) {
x++;
}
template <class FUNC>
void add(int &x, FUNC f) {
f(x);
}
int main() {
int x = 0;
add(x, inc);
return 0;
}
So, I get "error: there is no corresponding function to call" add "".
+3
source to share
2 answers
inc
is a template, not a function. You need to go through inc<int>
:
template <class _T>
void inc(int &x) {
x++;
}
template <class FUNC>
void add(int &x, FUNC f) {
f(x);
}
int main() {
int x = 0;
add(x, inc<int>);
return 0;
}
(Fixed a typo f
in the main function instead of adding.)
+6
source to share