【问题标题】:error passing a std::function vs a function pointer as a parameter将 std::function 与函数指针作为参数传递时出错
【发布时间】:2014-05-26 22:53:12
【问题描述】:

我有以下一段代码,其中我声明了 2 个将函数作为参数的函数(一个使用 std::function,另一个使用指向函数的指针)。作为参数的函数必须具有原型std::complex<double>(const std::complex<double>&)。此类参数的一个示例是 C++11 中的 std::sqrt,请参阅 http://en.cppreference.com/w/cpp/numeric/complex/sqrt 。现在,我完全不知道为什么采用指向函数的函数(即g_of_ten)有效,而另一个采用std::function 作为参数的函数却不起作用(即f_of_ten) .如果我取消注释该行

//cout << f_of_ten(std::sqrt) << endl; // ERROR here!!!

我得到了错误

 error: no matching function for call to 'f_of_ten'
        cout << f_of_ten(std::sqrt) << endl; // ERROR here!!!
                ^~~~~~~~
    /Users/vlad/minimal.cpp:10:6: note: candidate function not viable: no overload of 'sqrt' matching 'std::function<cplx (const cplx &)> &' for 1st argument
    cplx f_of_ten(std::function<cplx(const cplx &)>& x)
     ^
    1 error generated.

我完全不明白为什么会发生这种情况,但我认为 std::function 确实是各种函子(包括标准函数)的包装器。任何帮助都非常受欢迎! 下面是代码:

#include <cmath>
#include <complex>
#include <functional>
#include <iostream>

using namespace std;

using cplx = complex<double>; // to save some typing

cplx f_of_ten(std::function<cplx(const cplx &)> x) // the problematic one
{
    return x(10);
}

cplx g_of_ten(cplx(*x)(const cplx &)) // this works
{
    return (*x)(10);
}


int main()
{
    //cout << f_of_ten(std::sqrt) << endl; // compile ERROR here!!!
    cout << g_of_ten(std::sqrt) << endl;
}

PS:我当然也试过cout &lt;&lt; f_of_ten(&amp;std::sqrt) &lt;&lt; endl;,同样的故事,编译时出错。

@Yakk,

这很好用:

#include <cmath>
#include <complex>
#include <functional>
#include <iostream>
#include <valarray>

using cplx = std::complex<double>; // to save some typing

cplx f_of_ten(std::function<cplx(const cplx &)> x) // the problematic one
{
    return x(10);
}

cplx g_of_ten(cplx(*x)(const cplx &)) // this works
{
    return (*x)(10);
}


int main()
{
    //cout << f_of_ten(std::sqrt<double>) << endl; // compile ERROR here!!!
    std::cout << g_of_ten(std::sqrt) << std::endl;
}

【问题讨论】:

  • 问题是模板参数推导不会实例化模板来寻找匹配所以你需要使用std::sqrt&lt;double&gt;
  • 好的,我现在才发现这个 :) 无论如何,谢谢,这确实是问题。
  • @PeterT 不能保证std::sqrt 存在作为template 函数,或者有一个函数,或者它的第一个template 参数是它的参数的类型。
  • @Yakk 他正在使用这个函数en.cppreference.com/w/cpp/numeric/complex/sqrt 这是一个模板函数
  • @Yakk,至少他们在标准 C++11 中是这么说的,std::sqrt 是为复数模板化的。

标签: c++ c++11 function-pointers std-function


【解决方案1】:

std::sqrt 的重载不止一个,而且您的代码不知道如何确定您想要哪个。

试试:

typedef cplx(*complex_function)(const cplx &);

std::cout << f_of_ten( complex_function( std::sqrt ) ) << std::endl;

【讨论】:

    【解决方案2】:

    坏主意,查看 cmets 并编辑!

    复数的std::sqrt 是一个模板,如果您使用std::sqrt&lt;double&gt;,您的代码就可以工作。

    不确定为什么 g... 变体对你有用。

    编辑以挽救这个答案:

    您可以使用 lambda 来选择正确的 sqrt 像

    std::cout << f_of_ten([](cplx x){return std::sqrt(x);}) << std::endl;
    

    现在将进行模板参数推导和重载解析,选择正确的sqrt并将其打包到绑定到std::function的lambda中。

    【讨论】:

    • 是的,如果std::function不能推导出T,为什么指针版本可以这样呢?
    • @vsoftco #include &lt;valarray&gt; 和上面的失败,因为 no 保证只有一个 template&lt;class T&gt;sqrt 函数,上面是脆弱的,随时可能破坏无缘无故。
    • @Yakk Welp,那么我的回答是垃圾,会删除。感谢您的反馈。
    • @Yakk,看看我更新的代码,即使包含valarray 也能正常工作
    • @vsoftco 欢迎使用合法性边缘的东西:这就是为什么答案很脆弱(一个编译器工作,另一个不工作),所以即使它在您当前的环境中工作也是一个坏主意.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 2020-11-08
    • 1970-01-01
    • 2017-06-09
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多