在函数 C++ 中调用模板函数

Calling template function inside function C++

我有类似的情况,但更复杂。我试图在普通函数中调用模板函数,但我无法编译...

#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);
}

ioo是函数模板,不是函数,不能取地址

然而,这会起作用,因为它实例化了函数 void ioo<int>(int&):

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

正如 Jarod42 在评论中指出的那样,您可以将其变成 lambda:

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