为什么没有了以下工作:调试模板推演失败
std::string fn(int i)
{
sleep(i); // number of seconds to sleep for
std::cout << "Exiting after pausing " << i << " seconds\n";
return std::to_string(i+1);
}
template<typename T, typename U>
int simult(std::function<T(U)> f, std::vector<U> args)
{
return 666;
}
int main()
{
std::vector<int> args { 6, 5, 7 };
int r = simult(fn, args);
}
功能明显SIMULT是不完整的:我只是想获得的东西在这个阶段进行编译。这个想法是你提供了一个函数和一个参数列表,而simult会使用线程调用将这个参数分段地应用到f。
在这个阶段,我只是想弄清楚编译问题。
simult.cc: In function ‘int main()’:
simult.cc:43:25: error: no matching function for call to ‘simult(std::__cxx11::string (&)(int), std::vector<int>&)’
int r = simult(fn, args);
^
simult.cc:23:5: note: candidate: template<class T, class U> int simult(std::function<T(U)>, std::vector<U>)
int simult(std::function<T(U)> f, std::vector<U> args)
^~~~~~
simult.cc:23:5: note: template argument deduction/substitution failed:
simult.cc:43:25: note: mismatched types ‘std::function<T(U)>’ and ‘std::__cxx11::string (*)(int) {aka std::__cxx11::basic_string<char> (*)(int)}’
int r = simult(fn, args);
^
你没有真正给编译器的帮助!首先传入一个函数指针,然后它必须分配给一个'std :: function <>'来确定模板类型? – Nim
Ha。我总是使用666作为测试整数。基于这些理由,我喜欢这个,再加上一个。 – Bathsheba
@Bathsheba居然在80%的神圣文本中的数字是616而不是666,所以你没有什么可担心的... – 101010