如何将自动变量作为输入传递给另一个函数
How to pass an Auto variable as input to another function
我真的很想传递一个 auto
(函数)变量作为另一个函数的输入。
这是一个为我的 xroot f
:
接收参数的结构
struct my_f_params {
double flag;
auto inter_auto(double x, double y);
};
这是我真正想要的功能(它基于 GSL 库,因此无法更改形式)。我希望能够将“函数”作为变量传递,我猜它会发生的唯一方法是 auto
.
通过后,我尝试将其存储到一个新的auto
,但出现错误(见下文):
double xrootf (double x, void * p)
{
my_f_params * params = (my_f_params *)p;
double flag = (params->flag);
auto inter_auto = (params->inter_auto);
return flag*inter_auto(x);
}
这是一个 auto
函数,return 是一个 auto
函数。这非常有效(如果 xrootf
被评论,我可以打印例如 new_f(2)(2)
,等等):
auto new_f(double x){
auto in_result = [](double x, double y){
return x*y;
};
using namespace std::placeholders;
auto result_f = std::bind(in_result,x,_1);
return result_f;
}
证明auto
函数new_f
运行良好的测试代码:
int main(int argc, char const *argv[])
{
auto nest_f = new_f(-0.5);
printf("%f\n", nest_f(+2));
return 0;
}
将 auto
函数重铸为 double
也不起作用(对于代码的结构部分)。
我得到的错误是:
auto_pass.cpp: In function 'double xrootf(double, void*)':
auto_pass.cpp:28:42: error: unable to deduce 'auto' from 'params->my_f_params::inter_auto'
28 | auto inter_auto = (params->inter_auto);
| ^
auto_pass.cpp:28:42: note: couldn't deduce template parameter 'auto'
代码的目的是这样的:
有一个函数能够return一个函数(DONE W/new_f)
有一个能够将函数作为变量的函数(带有 new_f 的函数)(未完成)
编辑:这是一个快速的Python脚本,很容易实现我所说的:
def new_f(y):
#make any number of computatioanly costly Algebra with y
def g(x):
return x*y
return g
def xroot(f,flag):
return flag-f(flag)
auto
只是编译器推导类型的占位符,具体取决于使用 auto
的上下文。
在您的示例中,您不能使用 auto
作为 my_f_params::inter_auto()
的 return 值,因为编译器无法知道 inter_auto()
实际上是什么类型returns,所以它无法推断出 auto
的类型。您需要这样做:
struct my_f_params {
double flag;
auto inter_auto(double x, double y) { return ...; }
};
然后编译器可以从 return
语句中推断出 auto
的类型。
如果没有内联代码,您将必须明确 return 类型,例如:
struct my_f_params {
double flag;
double inter_auto(double x, double y);
};
double my_f_params::inter_auto(double x, double y) {
return ...;
}
但无论如何,这都不是你真正想要的。您的 xrootf()
函数试图仅使用一个参数调用 inter_auto()
,但 my_f_params::inter_auto()
被声明为采用 2 个参数。根据您显示的 Python 脚本,您真正想要的是 inter_auto
成为对其他一些外部函数的引用。在这种情况下,您可以为此目的使用 std::function
(根本不需要将 std::bind()
与 lambda 一起使用)。
试试这个:
#include <iostream>
#include <functional>
struct my_f_params {
double flag;
std::function<double(double)> inter_auto;
};
double xrootf(double x, void * p)
{
my_f_params * params = static_cast<my_f_params*>(p);
return params->flag * params->inter_auto(x);
}
auto new_f(double x){
return [x](double y) {
return x * y;
};
}
int main(int argc, char const *argv[])
{
my_f_params p;
p.flag = 123.45;
p.inter_auto = new_f(-0.5);
std::cout << xrootf(+2, &p) << std::endl;
return 0;
}
当调用 xrootf()
时,得到的等式将是:
flag * (x * y)
在这个例子中是:
123.45 * (-0.5 * +2) = -123.45
我真的很想传递一个 auto
(函数)变量作为另一个函数的输入。
这是一个为我的 xroot f
:
struct my_f_params {
double flag;
auto inter_auto(double x, double y);
};
这是我真正想要的功能(它基于 GSL 库,因此无法更改形式)。我希望能够将“函数”作为变量传递,我猜它会发生的唯一方法是 auto
.
通过后,我尝试将其存储到一个新的auto
,但出现错误(见下文):
double xrootf (double x, void * p)
{
my_f_params * params = (my_f_params *)p;
double flag = (params->flag);
auto inter_auto = (params->inter_auto);
return flag*inter_auto(x);
}
这是一个 auto
函数,return 是一个 auto
函数。这非常有效(如果 xrootf
被评论,我可以打印例如 new_f(2)(2)
,等等):
auto new_f(double x){
auto in_result = [](double x, double y){
return x*y;
};
using namespace std::placeholders;
auto result_f = std::bind(in_result,x,_1);
return result_f;
}
证明auto
函数new_f
运行良好的测试代码:
int main(int argc, char const *argv[])
{
auto nest_f = new_f(-0.5);
printf("%f\n", nest_f(+2));
return 0;
}
将 auto
函数重铸为 double
也不起作用(对于代码的结构部分)。
我得到的错误是:
auto_pass.cpp: In function 'double xrootf(double, void*)':
auto_pass.cpp:28:42: error: unable to deduce 'auto' from 'params->my_f_params::inter_auto'
28 | auto inter_auto = (params->inter_auto);
| ^
auto_pass.cpp:28:42: note: couldn't deduce template parameter 'auto'
代码的目的是这样的:
有一个函数能够return一个函数(DONE W/new_f)
有一个能够将函数作为变量的函数(带有 new_f 的函数)(未完成)
编辑:这是一个快速的Python脚本,很容易实现我所说的:
def new_f(y):
#make any number of computatioanly costly Algebra with y
def g(x):
return x*y
return g
def xroot(f,flag):
return flag-f(flag)
auto
只是编译器推导类型的占位符,具体取决于使用 auto
的上下文。
在您的示例中,您不能使用 auto
作为 my_f_params::inter_auto()
的 return 值,因为编译器无法知道 inter_auto()
实际上是什么类型returns,所以它无法推断出 auto
的类型。您需要这样做:
struct my_f_params {
double flag;
auto inter_auto(double x, double y) { return ...; }
};
然后编译器可以从 return
语句中推断出 auto
的类型。
如果没有内联代码,您将必须明确 return 类型,例如:
struct my_f_params {
double flag;
double inter_auto(double x, double y);
};
double my_f_params::inter_auto(double x, double y) {
return ...;
}
但无论如何,这都不是你真正想要的。您的 xrootf()
函数试图仅使用一个参数调用 inter_auto()
,但 my_f_params::inter_auto()
被声明为采用 2 个参数。根据您显示的 Python 脚本,您真正想要的是 inter_auto
成为对其他一些外部函数的引用。在这种情况下,您可以为此目的使用 std::function
(根本不需要将 std::bind()
与 lambda 一起使用)。
试试这个:
#include <iostream>
#include <functional>
struct my_f_params {
double flag;
std::function<double(double)> inter_auto;
};
double xrootf(double x, void * p)
{
my_f_params * params = static_cast<my_f_params*>(p);
return params->flag * params->inter_auto(x);
}
auto new_f(double x){
return [x](double y) {
return x * y;
};
}
int main(int argc, char const *argv[])
{
my_f_params p;
p.flag = 123.45;
p.inter_auto = new_f(-0.5);
std::cout << xrootf(+2, &p) << std::endl;
return 0;
}
当调用 xrootf()
时,得到的等式将是:
flag * (x * y)
在这个例子中是:
123.45 * (-0.5 * +2) = -123.45