更新时间:2022-02-05 23:06:05
我不这么认为,只是因为 boost::bind
在这种情况下正在寻找一个函数指针,而不是一个函数模板.当您传入 FCall2Templ
时,编译器会实例化该函数并将其作为函数指针传递.
I don't think so, only because boost::bind
in this case is looking for a function pointer, not a function template. When you pass in FCall2Templ<int, int>
, the compiler instantiates the function and it is passed as a function pointer.
但是,您可以使用函子执行以下操作
However, you can do the following using a functor
struct FCall3Templ {
template<typename ARG1, typename ARG2>
ARG1 operator()(ARG1 arg1, ARG2 arg2) {
return arg1+arg2;
}
};
int main() {
boost::bind<int>(FCall3Templ(), 45, 56)();
boost::bind<double>(FCall3Templ(), 45.0, 56.0)();
return 0;
}
您必须指定返回类型,因为返回类型与输入相关.如果返回没有变化,那么你可以在模板中添加typedef T result_type
,这样bind就可以确定结果是什么
You have to specify the return type, since the return type is tied to the inputs. If the return doesn't vary, then you can just add typedef T result_type
to the template, so that bind can determine what the result is