c++ class 模板可以实例化,但是具有相同模板参数的函数模板实例化失败
c++ class template can be instantiated but a function template instantiation with the same template parameters fails
我有一个用于绑定函数调用的包装器 class(一个帮助程序 class 来解决一些遗留代码问题):
template <class Result, class... Args>
class FunctionWrapper
{
std::function<Result()> func_;
public:
FunctionWrapper(std::function<Result(Args...)> f, Args&&... args) :
func_(std::bind(f, std::forward<Args>(args)...))
{
}
//...some methods using that func_
};
我可以编写以下代码,编译并运行良好:
double f(int i, double d)
{
return i*d;
}
//...
FunctionWrapper<double, int, double> w(f, 2, 4.5);
//calling methods of w ...
现在我想在定义包装器实例时节省一些输入,所以我引入了 make_wrapper
函数:
template <class Result, class... Args>
FunctionWrapper<Result, Args...> make_wrapper(std::function<Result(Args...)> f, Args&&... args)
{
return FunctionWrapper<Result, Args...>(f, std::forward<Args>(args)...);
}
尽管此函数的模板参数列表与包装器 class 的模板参数列表相同,但无法编译(将模板参数添加到 "help" 编译器也无济于事) :
auto w1=make_wrapper(f, 2, 4.5); //error: no matching function for call to 'make_wrapper', candidate template ignored: could not match 'function<type-parameter-0-0 (type-parameter-0-1...)>' against 'double (*)(int, double)'
auto w2=make_wrapper<double, int, double>(f, 2, 4.5); //error: no matching function for call to 'make_wrapper', candidate template ignored: could not match 'function<double (int, double, type-parameter-0-1...)>' against 'double (*)(int, double)'
编译器是 LLVM 6.1(当前的 XCode)。那么,这是怎么回事?是否可以修复 make
函数?
问题是编译器无法匹配参数的类型,这里是double(int, double)
到std::function<double(int,double)>
,这里没有自动转换!
你需要一个函数指针,而不是一个std::function作为参数:
template <class Result, class... Args>
FunctionWrapper<Result, Args...> make_wrapper(Result(*f)(Args...), Args&&... args)
{
return FunctionWrapper<Result, Args...>(f, std::forward<Args>(args)...);
}
问题是您对 make_wrapper()
的第一个参数没有您声称的类型。尽管函数指针可转换为相应的 std::function<...>
,但编译器不会使用 std::function<...> to deduce template arguments. Even if you'd make it a nested type to have
Args...be deduce by the other argument, the
Result` 类型,无法通过转换推导。
如果你真的只是想绑定函数指针,它应该可以期望函数指针作为参数:
template <class Result, class... Args>
FunctionWrapper<Result, Args...>
make_wrapper(Result (*f)(Args...), Args&&... args)
{
return FunctionWrapper<Result, Args...>(f, std::forward<Args>(args)...);
}
当函数指针的参数和传递的参数实际不一致时,可能需要为函数参数和要绑定的参数有单独的模板参数列表:
template <class Result, class... FArgs, class... Args>
FunctionWrapper<Result, FArgs...>
make_wrapper(Result (*f)(FArgs...), Args&&... args)
{
return FunctionWrapper<Result, FArgs...>(f, std::forward<Args>(args)...);
}
我可能会选择一个替代方案,它并不真正关心函数对象参数的实际类型,只是推导生成的任何函数类型:
template <class Fun, class... Args>
auto make_wrapper(Fun fun, Args&&... args)
-> FunctionWrapper<decltype(fun(std::forward<Args>(args)...)), Args...>
{
return FunctionWrapper<decltype(fun(std::forward<Args>(args)...)), Args...>(f, std::forward<Args>(args)...);
}
我有一个用于绑定函数调用的包装器 class(一个帮助程序 class 来解决一些遗留代码问题):
template <class Result, class... Args>
class FunctionWrapper
{
std::function<Result()> func_;
public:
FunctionWrapper(std::function<Result(Args...)> f, Args&&... args) :
func_(std::bind(f, std::forward<Args>(args)...))
{
}
//...some methods using that func_
};
我可以编写以下代码,编译并运行良好:
double f(int i, double d)
{
return i*d;
}
//...
FunctionWrapper<double, int, double> w(f, 2, 4.5);
//calling methods of w ...
现在我想在定义包装器实例时节省一些输入,所以我引入了 make_wrapper
函数:
template <class Result, class... Args>
FunctionWrapper<Result, Args...> make_wrapper(std::function<Result(Args...)> f, Args&&... args)
{
return FunctionWrapper<Result, Args...>(f, std::forward<Args>(args)...);
}
尽管此函数的模板参数列表与包装器 class 的模板参数列表相同,但无法编译(将模板参数添加到 "help" 编译器也无济于事) :
auto w1=make_wrapper(f, 2, 4.5); //error: no matching function for call to 'make_wrapper', candidate template ignored: could not match 'function<type-parameter-0-0 (type-parameter-0-1...)>' against 'double (*)(int, double)'
auto w2=make_wrapper<double, int, double>(f, 2, 4.5); //error: no matching function for call to 'make_wrapper', candidate template ignored: could not match 'function<double (int, double, type-parameter-0-1...)>' against 'double (*)(int, double)'
编译器是 LLVM 6.1(当前的 XCode)。那么,这是怎么回事?是否可以修复 make
函数?
问题是编译器无法匹配参数的类型,这里是double(int, double)
到std::function<double(int,double)>
,这里没有自动转换!
你需要一个函数指针,而不是一个std::function作为参数:
template <class Result, class... Args>
FunctionWrapper<Result, Args...> make_wrapper(Result(*f)(Args...), Args&&... args)
{
return FunctionWrapper<Result, Args...>(f, std::forward<Args>(args)...);
}
问题是您对 make_wrapper()
的第一个参数没有您声称的类型。尽管函数指针可转换为相应的 std::function<...>
,但编译器不会使用 std::function<...> to deduce template arguments. Even if you'd make it a nested type to have
Args...be deduce by the other argument, the
Result` 类型,无法通过转换推导。
如果你真的只是想绑定函数指针,它应该可以期望函数指针作为参数:
template <class Result, class... Args>
FunctionWrapper<Result, Args...>
make_wrapper(Result (*f)(Args...), Args&&... args)
{
return FunctionWrapper<Result, Args...>(f, std::forward<Args>(args)...);
}
当函数指针的参数和传递的参数实际不一致时,可能需要为函数参数和要绑定的参数有单独的模板参数列表:
template <class Result, class... FArgs, class... Args>
FunctionWrapper<Result, FArgs...>
make_wrapper(Result (*f)(FArgs...), Args&&... args)
{
return FunctionWrapper<Result, FArgs...>(f, std::forward<Args>(args)...);
}
我可能会选择一个替代方案,它并不真正关心函数对象参数的实际类型,只是推导生成的任何函数类型:
template <class Fun, class... Args>
auto make_wrapper(Fun fun, Args&&... args)
-> FunctionWrapper<decltype(fun(std::forward<Args>(args)...)), Args...>
{
return FunctionWrapper<decltype(fun(std::forward<Args>(args)...)), Args...>(f, std::forward<Args>(args)...);
}