Home > Net >  Calling template function inside function C
Calling template function inside function C

Time:10-02

I have similar case but more complex. I am trying to call a template function inside a normal function but I can't compile...

#include <iostream>

using namespace std;

template<class T>
void ioo(T& x) { std::cout << x << "\n"; }

template<class T, class ReadFunc>
void f(T&& param, ReadFunc func) {
    func(param);
}

int main() {
    int x = 1;
    std::string y = "something";
    f(x, &::ioo);
}

CodePudding user response:

ioo is a function template, not a function, so you can't take its address.

This would however work since it instantiates the function void ioo<int>(int&):

f(x, &ioo<decltype(x)>);

and as noted by Jarod42 in the comments, you could make it into a lambda:

f(x, [](auto& arg){ioo(arg);});
  • Related