C++ C++;std::functional中的可变函数模板
基本上我想用这个递归积分多维积分。C++ C++;std::functional中的可变函数模板,c++,templates,metaprogramming,variadic-templates,std-function,C++,Templates,Metaprogramming,Variadic Templates,Std Function,基本上我想用这个递归积分多维积分。 但问题本身是一个普遍的问题。这不是针对集成的 #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){ retur
但问题本身是一个普遍的问题。这不是针对集成的
#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;
}
#包括“math.h”
#包括
#包括
双f(双x,双y,双z){
返回x+y+z+1;
}
//基地
双redDim(标准::函数&f){
返回f(0);//用于测试的愚蠢积分器
}
//递归
模板
自动redDim(标准::函数&f){
返回redDim([=](Trest…R){返回redDim([=](double x){返回f(x,R…;});});
}
int main(){
std::cout函数指针不是std::function
模板参数推导不进行任何类型转换,只进行一些基本情况的转换
添加一个调用std函数one的redim(double(*f)(Args…)
模板
template<class...Args>
auto redDim( double(*f)(Args...) {
return redDim( std::function<double(Args...)>{ f } );
}
另外,将最后一个
redDim
的参数更改为std::function
(非参考)。或者,根据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;
}
#包括“math.h”
#包括
#包括
双f(双x,双y,双z){
返回x+y+z+1;
}
//基地
双redDim(const std::function&f){
返回f(0);//用于测试的愚蠢积分器
}
//递归
模板
自动redDim(const std::function&f){
返回雷迪姆(
std::函数{
[=](Trest…R)->double{return redDim([=](double x){return f(x,R…;});});
}
模板
自动redDim(双(*f)(参数…){
返回redDim(std::function{f});
}
int main(){
std::这能回答你的问题吗?现在下一个错误是:auto-redDim(double(*)(Args…)[with-Args={double,double,double}]需要错误:没有匹配的函数调用'redDim(redDim(std::function)[with-Tfirst=double;Trest={double,double}]:)'返回redDim([&](Trest…rest){return redDim([&](double x){return f(x,rest…;});});@Babalion-Live-example添加。
// 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...);}
}
);
}
}
);
}
#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;
}