我正在尝试将回调函数作为函数参数传递。但是在以下代码中出现模板替换失败错误。不确定模板替换失败的原因。
#include<iostream>
#include <map>
#include <tuple>
#include <functional>
template<typename A,typename B>
void myfun(std::map<A,B> & mm, std::function<std::tuple<A,B>(void)> fn)
{
A key;
B val;
std::tie(key,val) = fn();
mm[key] = val;
}
std::tuple<std::string,int> fun()
{
return std::make_tuple(std::string("hi"),1);
}
int main()
{
std::map<std::string,int> gg;
#if 0
//fixed version
std::function<std::tuple<std::string,int>(void)> yy = fun;//fixed
myfun(gg,yy);//fixed
#else
// error causing code
myfun(gg,fun);
#endif
}
错误如下
main.cpp:8:6: note: template argument deduction/substitution failed:
main.cpp:25:17: note: mismatched types 'std::function<std::tuple<_T1, _T2>()>' and 'std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> (*)()'
myfun(gg,fun);
最佳答案
编译器不能同时转换为 std::function
和推导模板参数。它不理解任意函数指针和 std::function
之间的映射。
有几种方法可以解决这个问题。
您可以在调用站点显式创建一个 std::function
:
myfun(gg,std::function<std::tuple<std::string,int>(void)>{fun});`
您可以编写一个make_function
函数来为您推断类型。您可以在线找到关于此的讨论和实现,例如 here , here和 here .
myfun(gg,make_function(fun));
您可以忘记 std::function
并推导整个函数类型。这是我会采用的方法:
template<typename A,typename B, typename Fun>
void myfun(std::map<A,B> & mm, Fun fn)
{
A key;
B val;
std::tie(key,val) = fn();
mm[key] = val;
}
关于c++ - std::function 的模板替换失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33099113/