简体   繁体   中英

C++ variadic function template in std::functional

basically I want to integrate a multi-dimensional integral with this recursion.
But the problem itself is a general one. It's not specific for integration.

#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;
}

The problem is, compiler says:

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

So why is the type of f not matching the requirements of redDim() ?
Thus I can't even test, if my method works.
I hope you could help me!

A function pointer is not a std::function .

Template argument deduction does not do any type conversions, other than a few to-base cases.

Add a redim( double(*f)(Args...) ) template that calls the std function one.

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

and it should work.

This can deduce the signature of the function pointer. It then explicitly converts to a std::function , which then matches your other redim functions.

You do have to explicitly convert your other lambdas to std::function s:

// Recursion
template<typename Tfirst, typename... Trest>
auto redDim(std::function<double(Tfirst first,Trest... rest)> f){
  return redDim(
    std::function<double(Trest...)>{
      [=](Trest... R){
        return redDim(
          std::function<double(double)>{
            [=](double x){return f(x,R...);}
          }
        );
      }
    }
  );
}

Live example .

Also change the argument for the last redDim to std::function<double(double)> (not a reference). Alternatively, a const& .

Based on Yakk - Adam Nevraumont's answer, the following code should compile:

#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;
}

As Yakk already pointed out, std::function and a function pointer don't have the same type. Please note that I've also changed the argument type of redDim to const std::function<...> &) . You could also pass rvalue references using the && syntax.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM