首页 > 解决方案 > 将参数包与 std::function 一起使用时出错

问题描述

我有这个代码:

#include <functional>
template<class T, class U>
void f(std::function<T(U)> g) {};
int main() {
    auto h = [](int x) {return x;};
    f<int, int>(h);
}

此代码有效。但是当我使用参数包(下面的代码)时,它失败了......为什么?

#include <functional>
template<class T, class...U>
void f(std::function<T(U...)> g) {};
int main() {
    auto h = [](int x) {return x;};
    f<int, int>(h);
}

使用std::function<int(int)>而不是auto使其与参数包一起使用。

标签: c++templateslambdac++17variadic-templates

解决方案


推荐阅读