繁体   English   中英

c++ 类模板可以实例化,但具有相同模板参数的函数模板实例化失败

[英]c++ class template can be instantiated but a function template instantiation with the same template parameters fails

我有一个用于绑定函数调用的包装类(一个帮助类来解决一些遗留代码问题):

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

尽管此函数的模板参数列表与无法编译的包装类之一相同(将模板参数添加到“帮助”编译器也无济于事):

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功能?

问题是您对make_wrapper()第一个参数没有您声称的类型。 尽管函数指针可以转换为相应的std::function<...>但编译器不会使用std::function<...> to deduce template arguments. Even if you'd make it a nested type to have 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)...);
}

问题是编译器无法匹配参数的类型,这里是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)...);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM