【发布时间】:2021-11-12 15:41:41
【问题描述】:
我有以下代码打算采用一个通用函数对象,该对象接受两个参数并返回一个函数对象,该函数对象以其他顺序对参数执行相同的操作。
#include <type_traits>
#include <functional>
template<typename Function, typename FirstIn
, typename SecondIn, typename std::enable_if<std::is_invocable<Function, FirstIn, SecondIn>::value>::type>
std::function<typename std::invoke_result<Function, FirstIn, SecondIn>::type(SecondIn, FirstIn)>
swapInput(Function f)
{
return[=](SecondIn b, FirstIn a) { return std::invoke(f, a, b); };
}
int main()
{
std::function<bool(std::string, int)> isLength = [](std::string s, int len) {return (s.size() == len); };
std::function<bool(int, std::string)> lengthIs =
swapInput<std::function<bool(std::string, int)>, std::string, int>(isLength);
}
这会在分配lengthIs 的行出现以下编译器错误:
Error C2783 'std::function<std::invoke_result<Function,FirstIn,SecondIn>::type(SecondIn,FirstIn)> swapInput(Function)'
: could not deduce template argument for '__formal'
Error C2672 'swapInput': no matching overloaded function found
我正在使用设置为 C++17 的 Visual Studio 19。
【问题讨论】:
标签: c++ templates lambda c++17 function-templates