我有以下代码可以正常工作。我试图理解语法。返回语句有 std::plus<double>()
.此处的 double 具有返回值数据类型。但是函数定义的返回类型为 std::function<double(double, double)>
这表示两个 double 参数。这两者有什么关系?
#include <functional>
#include <iostream>
using namespace std;
std::function<double(double, double)> GetFunction()
{
return std::plus<double>();
}
int main()
{
auto operation = GetFunction();
int a = operation(1, 4);
std::cout << std::plus<>{}(1, 4) << '\n';
return 0;
}
最佳答案
存在从std::plus<double>
的隐式转换至 std::function<double(double,double)>
, 因为前者有一个成员 call operator double operator()(double, double)
.请参阅 std::function constructors 的文档.
关于c++11 - C++例程返回函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37649678/