我有一段有点做作的代码:
#include <functional>
template <typename... ARGs>
auto construct1(std::function<void(ARGs...)> p, const ARGs &...args) {}
template < typename... ARGs>
auto construct2(std::function<void(int)> p, const ARGs &...args) {}
int main() {
auto p = [](int) {};
construct1<int>(p, 0);
construct2<int>(p, 0);
return 0;
}
ARGs... = { int }
为什么编译器在第一种情况下努力解决这个问题?std::function<void(int)>
如果我通过在签名中使用来帮助编译器(第二种情况),则代码可以编译。在这两种情况下,编译器都可以毫不费力地推断出const ARGs&...
应该是const int&
. 使用 C++17。
海合会:
main.cpp: In function ‘int main()’:
main.cpp:11:25: error: no matching function for call to ‘construct1<int>(main()::<lambda(int)>&, int)’
11 | construct1<int>(p, 0);
| ^
main.cpp:4:6: note: candidate: ‘template<class ... ARGs> auto construct1(std::function<void(ARGs ...)>, const ARGs& ...)’
4 | auto construct1(std::function<void(ARGs...)> p, const ARGs &...args) {}
| ^~~~~~~~~~
main.cpp:4:6: note: template argument deduction/substitution failed:
main.cpp:11:25: note: ‘main()::<lambda(int)>’ is not derived from ‘std::function<void(ARGs ...)>’
11 | construct1<int>(p, 0);
|
铛:
main.cpp:11:5: error: no matching function for call to 'construct1'
construct1<int>(p, 0);
^~~~~~~~~~~~~~~
main.cpp:4:6: note: candidate template ignored: could not match 'function<void (int, type-parameter-0-0...)>' against '(lambda at main.cpp:10:14)'
auto construct1(std::function<void(ARGs...)> p, const ARGs &...args) {}
^
1 error generated.