[英]C++ template class to wrap a whatever function
我想包装任何输入/输出类型的函数。 下面我尝试使用C ++模板。
double foo(double x){return x*x;}
template <typename funct>
class TestFunction{
public:
TestFunction(const funct& userFunc): f(userFunc){}
private:
const funct& f;
};
template <typename funct>
TestFunction<funct> createTestFunction(const funct& f){
return TestFunction<funct>(f);
}
int main(){
TestFunction<> testFunc=createTestFunction(foo);
}
编译该程序会给我错误消息:
too few template arguments for class template 'TestFunction'
为什么C ++编译器无法推断TestFunction <>的类型? 我该如何解决? 谢谢。 另外,还有没有那么尴尬的方法呢?
TestFunction<> testFunc = createTestFunction(foo);
应该
auto testFunc = createTestFunction(foo);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.