I have a function defined this way:
(我有这样定义的函数:)
template <size_t SIZE>
double MyFun(std::function<double(std::array<double,SIZE>&)> f, std::array<double,SIZE> &x, std::array<double,SIZE> &y){
instructions;
}
and another function, which should be the argument of the previous, defined like this:
(另一个函数,应该是前一个的参数,定义如下:)
double MyFun2(std::array<double,3> &var){
instructions;
}
If I try to call "MyFun" like this:
(如果我尝试这样称呼“ MyFun”:)
double something;
std::array<double,3> var = {...};
std::array<double,3> var2 = {...};
something = MyFun(MyFun2, var, var2);
I get this error:
(我收到此错误:)
error: no matching function for call to ‘MyFun(double (&)(std::array<double, 3>&), std::array<double, 3>&, std::array<double, 3>&)’
note: template argument deduction/substitution failed:
note: mismatched types ‘std::function<double(std::array<double, SIZE>&)>’ and ‘double (*)(std::array<double, 3>&)’
Moreover, if I try to store "MyFun2" in a variable with "auto"
(此外,如果我尝试将“ MyFun2”存储在带有“自动”的变量中)
auto function = MyFun2;
the type given to "function" is:
(给“功能”的类型是:)
double (*function)(std::array<double, 3UL> &var)
and I can't use "function" as a parameter for MyFun aswell.
(而且我也不能将“功能”用作MyFun的参数。)
The only solution I've found is to somewhat cast "MyFun2" in the right type by specifying it: (我发现的唯一解决方案是通过指定正确的类型来对“ MyFun2”进行某种类型的转换:)
double something;
std::array<double,3> var = {...};
std::array<double,3> var2 = {...};
std::function<double(std::array<double,3>&)> function = MyFun2;
something = MyFun(function, var, var2);
This way, passing "function" as the first parameter of "MyFun" works.
(这样,将“功能”作为“ MyFun”的第一个参数传递即可。)
But why do I have such an ambiguity and I can't call MyFun by just typing MyFun2 as the first parameter? (但是,为什么我会有这样的歧义,而不能仅通过输入MyFun2作为第一个参数来调用MyFun?)
And why auto won't figure out the "right" type? (为什么汽车不知道“正确”类型?)
Thank you :)
(谢谢 :))
ask by LucioPhys translate from so