[英]no matching function call for variadic template function
the code is as follows代码如下
#include <iostream>
#include <functional>
using namespace std;
template<class F, class ...Args>
result_of_t<F> foo(F&& f,Args&&... args){
cout<<sizeof...(args);
f(args...);
}
int main(){
foo([](char a){ cout<<a<<'\n'; },'a');
return 0;
}
while I compile the code, it says当我编译代码时,它说
template.cpp:12:38: error: no matching function for call to ‘foo(main()::<lambda(char)>, char)’
the full compilation error is as follows完整的编译错误如下
template.cpp: In function ‘int main()’:
template.cpp:12:38: error: no matching function for call to ‘foo(main()::<lambda(char)>, char)’
12 | foo([](char a){ cout<<a<<'\n'; },'a');
| ^
template.cpp:6:16: note: candidate: ‘template<class F, class ... Args> std::result_of_t<F> foo(F&&, Args&& ...)’
6 | result_of_t<F> foo(F&& f,Args&&... args){
| ^~~
template.cpp:6:16: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/10.2.0/bits/move.h:57,
from /usr/include/c++/10.2.0/bits/nested_exception.h:40,
from /usr/include/c++/10.2.0/exception:148,
from /usr/include/c++/10.2.0/ios:39,
from /usr/include/c++/10.2.0/ostream:38,
from /usr/include/c++/10.2.0/iostream:39,
from template.cpp:1:
/usr/include/c++/10.2.0/type_traits: In substitution of ‘template<class _Tp> using result_of_t = typename std::result_of::type [with _Tp = main()::<lambda(char)>]’:
template.cpp:6:16: required by substitution of ‘template<class F, class ... Args> std::result_of_t<F> foo(F&&, Args&& ...) [with F = main()::<lambda(char)>; Args = {char}]’
template.cpp:12:38: required from here
/usr/include/c++/10.2.0/type_traits:2570:11: error: invalid use of incomplete type ‘class std::result_of<main()::<lambda(char)> >’
2570 | using result_of_t = typename result_of<_Tp>::type;
| ^~~~~~~~~~~
/usr/include/c++/10.2.0/type_traits:2344:11: note: declaration of ‘class std::result_of<main()::<lambda(char)> >’
2344 | class result_of;
| ^~~~~~~~~
why the first statement of main function cannot match the function?为什么主函数的第一条语句不能匹配函数?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.