Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
518 views
in Technique[技术] by (71.8m points)

c++ - std ::带模板的函数,类型问题,没有匹配的调用函数(std::function with template, type problem, no matching function for call)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You're getting the no matching function for call to... error std::function doesn't have deduction guides for deducing the signature of a function pointer.

(您将获得no matching function for call to...错误std::function没有推论指南,无法推论函数指针的签名。)

When you do this:

(执行此操作时:)

auto function = MyFun2;

The function, MyFun2 is decayed to a function pointer.

(函数MyFun2衰减到函数指针。)

It doesn't make sense to store the value of a function in a variable because, well, what is the value of a function?

(将函数的值存储在变量中没有任何意义,因为函数的值是多少?)

It's a block of machine instructions.

(这是一堆机器指令。)

Why would you ever want to copy that around?

(为什么要复制它?)

The language assumes that you won't so a function is decayed to a function pointer.

(该语言假设您不会,因此函数会退化为函数指针。)

The above is equivalent to this:

(以上等同于此:)

double (*function)(std::array<double, 3> &var) = MyFun2;

You found one solution to the error and that is to construct a std::function from the function pointer directly.

(您找到了解决该错误的方法,即直接从函数指针构造std::function 。)

Another solution would be to avoid std::function altogether.

(另一个解决方案是完全避免std::function 。)

template <size_t SIZE>
double MyFun(double (*f)(std::array<double, SIZE>&), std::array<double, SIZE> &x, std::array<double, SIZE> &y){
   instructions;
}

So now your original example works

(所以现在您的原始示例有效)

something = MyFun(MyFun2, var, var2);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...