没有匹配函数调用可变参数模板函数

代码如下

#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;
}

当我编译代码时,它说

template.cpp:12:38: error: no matching function for call to ‘foo(main()::<lambda(char)>, char)’

完整的编译错误如下

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;
      |           ^~~~~~~~~

为什么main函数的第一个语句不能与该函数匹配?

rafix07

因为foo不能推断出返回类型

result_of 需要函子的完整签名,Args ..在那里不存在。

template<class F, class ...Args>
result_of_t< F(Args...) > foo(F&& f,Args&&... args){
    cout<<sizeof...(args);
    f(args...);
}

演示版

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

可变参数模板,没有匹配的调用函数

可变参数模板函数:没有匹配的调用函数,std :: endl

C ++中没有参数可变参数模板函数

在C ++中没有匹配的构造函数来初始化可变参数模板

没有用于初始化可变参数模板类的匹配构造函数

基准可变参数模板函数调用

lambda 函数参数与模板函数没有匹配的函数调用

使用成员函数调用可变参数模板函数

是否可以在没有显式专门化的情况下调用可变参数模板函数?

C ++:如何在可变参数模板参数上调用带有类型参数的函数?

有时从可变参数模板函数中调用函数

带有可变参数模板构造函数的推导指南和可变参数类模板-参数包长度不匹配

C ++可变参数模板:无法匹配函数

具有模板函数名称的可变参数模板

使用可变参数类模板的模板参数调用可变参数函数模板?

调用类内的函数的C ++ 11可变参数模板

调用可变参数模板函数而无args失败

可变参数模板扩展中的函数调用顺序

带有可变参数模板参数的重载函数

具有可变参数模板参数的函数指针

以可变参数类模板作为函数调用参数的函数模板参数推导

CUDA 模板错误:没有函数模板实例与参数列表匹配

具有可变参数模板的成员函数指针

没有函数模板“ max”的实例与参数列表匹配的参数类型为(int,int)

为每个可变参数模板参数调用函数,并将结果作为构造函数参数传递

递归可变参数函数模板

编写可变参数模板构造函数

如何重载可变参数模板函数?

可变参数模板中的函数顺序