std :: functional中的C ++可变函数模板

巴巴利翁

基本上,我想将此递归集成多维积分。
但是问题本身是一个普遍的问题。它不是特定于集成的。

#include "math.h"
#include <iostream>
#include <functional>

double f(double x,double y,double z){
    return x+y+z+1;
}

//Base
double redDim(std::function<double(double)> &f){
    return f(0); //a silly integrator for testing
}
// Recursion
template<typename Tfirst=double, typename... Trest>
auto redDim(std::function<double(Tfirst first,Trest... rest)> &f){
    return redDim([=](Trest... R){return redDim([=](double x){return f(x,R...);});});
}

int main(){
    std::cout<<redDim(f)<<std::endl;
    return 0;
}

问题是,编译器说:

c:\C++\templateTutorial\templateTut.cpp: In function 'int main()':
c:\C++\templateTutorial\templateTut.cpp:24:19: error: no matching function for call to 'redDim(double (&)(double, double, double))'
     cout<<redDim(f)<<endl;
                   ^
c:\C++\templateTutorial\templateTut.cpp:12:8: note: candidate: 'double redDim(std::function<double(double)>&)'
 double redDim(std::function<double(double)> &f){
        ^~~~~~
c:\C++\templateTutorial\templateTut.cpp:12:8: note:   no known conversion for argument 1 from 'double(double, double, double)' to 'std::function<double(double)>&'
c:\C++\templateTutorial\templateTut.cpp:17:6: note: candidate: 'template<class Tfirst, class ... Trest> auto redDim(std::function<double(Tfirst, Trest ...)>&)'
 auto redDim(std::function<double(Tfirst first,Trest... rest)> &f){
      ^~~~~~
c:\C++\templateTutorial\templateTut.cpp:17:6: note:   template argument deduction/substitution failed:
c:\C++\templateTutorial\templateTut.cpp:24:19: note:   mismatched types 'std::function<double(Tfirst, Trest ...)>' and 'double(double, double, double)'
     cout<<redDim(f)<<endl;
                   ^
The terminal process terminated with exit code: 1

那么为什么类型f不符合要求redDim()
因此,即使我的方法可行,我什至无法测试。
我希望你能帮助我!

斯蒂芬·克塞尔

根据Yakk-Adam Nevraumont的回答,应编译以下代码:

#include "math.h"
#include <iostream>
#include <functional>

double f(double x,double y,double z){
    return x+y+z+1;
}

//Base
double redDim(const std::function<double(double)> &f){
    return f(0); //a silly integrator for testing
}
// Recursion
template<typename Tfirst=double, typename... Trest>
auto redDim(const std::function<double(Tfirst first,Trest... rest)> &f) {

    return redDim(
    std::function<double(Trest...)>{
        [=](Trest... R)->double{return redDim([=](double x){return f(x,R...);});}});
}

template<class...Args>
auto redDim( double(*f)(Args...)) {
  return redDim( std::function<double(Args...)>{ f } );
}

int main(){
    std::cout<<redDim(f)<<std::endl;
    return 0;
}

正如Yakk所指出的,std::function函数指针的类型不同。请注意,我还将参数类型更改redDimconst std::function<...> &)您也可以rvalue使用&&语法传递引用

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

std :: function中的可变参数模板参数匹配

计算可变参数模板元组中的 std::optional 类型

C ++ 11可变参数模板和std :: endl

可变函数模板中的歧义

可变参数模板中的函数顺序

std::invoke 不喜欢可变参数模板成员函数?

如何在SWIG中包装可变参数模板类的可变参数模板成员函数?

C++:函数模板指针的 std::vector

用可变参数模板在C ++中包装函数指针

C ++中没有参数可变参数模板函数

具有std :: enable_if的C ++可变参数模板部分模板专业化

C ++可变参数模板打包到std :: array中并解压缩

如何访问可变参数模板函数中的参数

可变参数模板类中的派生(虚拟)函数参数

可变参数模板扩展中的函数调用顺序

命名空间和类中的可变参数模板函数

限制可变参数模板类中的构造函数访问

C ++可变参数模板:无法匹配函数

C ++:可变参数模板和函数重载

调用类内的函数的C ++ 11可变参数模板

C ++ 11:可变参数模板函数参数的数量?

通过可变参数模板的C ++ 11构造函数继承

C ++ 11可变参数模板函数存储

std :: function到可变参数成员函数,然后绑定可变参数模板参数

可变参数模板构造中的隐式std :: pair构造

std :: pair分段构造器中的可变参数模板问题

在可变参数模板中从lambda隐式转换为std :: function

如何为std :: string可变参数模板参数调用c_str()?

C ++将std :: function对象传递给可变参数模板