Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
动态创建std::tuple并将其扩展到参数包中 我正在编写一个用C++编写的软件插件,这里有一个插件,定义了: extern“C” irods::ms_table_entry*plugin_factory(){ //它将接受的msParam_t*参数数 整数=2; irods::ms_table_entry*msvc=新的irods::ms_table_entry(numArguments); msvc->add_操作( “我的微服务”, std::功能(MyMicroservice) ); 返回msvc; }_C++_C++11_Variadic - Fatal编程技术网

动态创建std::tuple并将其扩展到参数包中 我正在编写一个用C++编写的软件插件,这里有一个插件,定义了: extern“C” irods::ms_table_entry*plugin_factory(){ //它将接受的msParam_t*参数数 整数=2; irods::ms_table_entry*msvc=新的irods::ms_table_entry(numArguments); msvc->add_操作( “我的微服务”, std::功能(MyMicroservice) ); 返回msvc; }

动态创建std::tuple并将其扩展到参数包中 我正在编写一个用C++编写的软件插件,这里有一个插件,定义了: extern“C” irods::ms_table_entry*plugin_factory(){ //它将接受的msParam_t*参数数 整数=2; irods::ms_table_entry*msvc=新的irods::ms_table_entry(numArguments); msvc->add_操作( “我的微服务”, std::功能(MyMicroservice) ); 返回msvc; },c++,c++11,variadic,C++,C++11,Variadic,我希望能够使用numArguments动态生成std::function参数包。其中numArguments表示msParam\u t*参数的数量 我不是C++专家(特别是模板),所以我发现通过实现以下可能: std::tuple std::tuple_cat std::index_序列 std::生成整数序列 但我真的不知道如何开始实施这个。我发现的例子很难理解,我无法将它们转化为我自己的需要。有人能提供一些提示、简短的例子或参考资料来说明这一点吗?任何信息都非常感谢 模板 templat

我希望能够使用
numArguments
动态生成
std::function
参数包。其中
numArguments
表示
msParam\u t*
参数的数量

<>我不是C++专家(特别是模板),所以我发现通过实现以下可能:
  • std::tuple
  • std::tuple_cat
  • std::index_序列
  • std::生成整数序列
但我真的不知道如何开始实施这个。我发现的例子很难理解,我无法将它们转化为我自己的需要。有人能提供一些提示、简短的例子或参考资料来说明这一点吗?任何信息都非常感谢

模板
template<typename T>
struct toFunc;

template<typename...T>
struct toFunc<std::tuple<T...>>
{
    using type = std::function<void(T...)>;
};

int main(int argc, char **argv) {

    using t = std::tuple<int, int, int>;
    using func = toFunc<t>::type;
    auto f = func([](int a, int b, int c){std::cout << a << b << c << std::endl;});
    f(1, 2, 3);
  return 0;
}
结构toFunc; 模板 结构toFunc { 使用type=std::function; }; int main(int argc,字符**argv){ 使用t=std::tuple; 使用func=toFunc::type; 自动f=func([](inta,intb,intc){std::cout
template
结构toFunc;
模板
结构toFunc
{
使用type=std::function;
};
int main(int argc,字符**argv){
使用t=std::tuple;
使用func=toFunc::type;

自动f=func([](整数a、整数b、整数c){std::cout我不知道下面的内容是否正是您想要的,但我认为您想要的是能够根据
MyMicroservice
所使用的参数数量为
std::function
生成正确的模板参数,这些参数存储在
numParameters
变量中

如果是这种情况,您可以完全省略编写它们,使用
decltype
,让编译器为您编写它们

int myMicroservice1(int a,int b, int c){
    return a+b+c;
}

int myMicroservice2(int a,int b, int c,int d){
    return a*b*c-d;
}

int myMicroservice3(int a,int b, int c,int d,int e, int f){
    return a*b*c+e+f;
}


template<typename... types_t>
void add_operation(const std::string & _op, std::function< int(types_t...)> _f ){

}   

int main() {
    add_operation("blabla",std::function<decltype(myMicroservice1)>(myMicroservice1));
    add_operation("blabla",std::function<decltype(myMicroservice2)>(myMicroservice2));
    add_operation("blabla",std::function<decltype(myMicroservice3)>(myMicroservice3));
    return 0;
}
intmymicroservice1(inta、intb、intc){
返回a+b+c;
}
int MyMicroService 2(int a、int b、int c、int d){
返回a*b*c-d;
}
intMyMicroService3(intA、intB、intC、intD、intE、intF){
返回a*b*c+e+f;
}
模板
void add_操作(const std::string&_op,std::function\u f){
}   
int main(){
添加_操作(“blabla”,std::function(myMicroservice1));
添加_操作(“blabla”,std::function(myMicroservice2));
添加_操作(“blabla”,std::function(myMicroservice3));
返回0;
}

我不知道以下内容是否正是您所要求的,但我认为您想要的是能够根据
MyMicroservice
获取的参数数量为
std::function
生成正确的模板参数,这些参数存储在
numParameters
变量中

如果是这种情况,您可以完全省略编写它们,使用
decltype
,让编译器为您编写它们

int myMicroservice1(int a,int b, int c){
    return a+b+c;
}

int myMicroservice2(int a,int b, int c,int d){
    return a*b*c-d;
}

int myMicroservice3(int a,int b, int c,int d,int e, int f){
    return a*b*c+e+f;
}


template<typename... types_t>
void add_operation(const std::string & _op, std::function< int(types_t...)> _f ){

}   

int main() {
    add_operation("blabla",std::function<decltype(myMicroservice1)>(myMicroservice1));
    add_operation("blabla",std::function<decltype(myMicroservice2)>(myMicroservice2));
    add_operation("blabla",std::function<decltype(myMicroservice3)>(myMicroservice3));
    return 0;
}
intmymicroservice1(inta、intb、intc){
返回a+b+c;
}
int MyMicroService 2(int a、int b、int c、int d){
返回a*b*c-d;
}
intMyMicroService3(intA、intB、intC、intD、intE、intF){
返回a*b*c+e+f;
}
模板
void add_操作(const std::string&_op,std::function\u f){
}   
int main(){
添加_操作(“blabla”,std::function(myMicroservice1));
添加_操作(“blabla”,std::function(myMicroservice2));
添加_操作(“blabla”,std::function(myMicroservice3));
返回0;
}

编译时是否知道
numArgument
?不可能动态执行此操作,因为这将涉及代码生成,但您可以静态生成代码或静态生成集合。添加操作的类型是什么?@DavideSpataro-否,
numArgument
将在运行时定义。它是否表示参数的数量对于
MyMicroservice
?如果它这样做,则在编译时就知道了。您不能在运行时做一些本应在编译时完成的事情…
std::make_index_sequence
需要在编译时已知参数,以便在编译时生成适当的
std::integer_sequence
,如果您不知道编译时的值告诉我,你注定要创建所有可能支持的选项,然后在运行时从以前预编译的类型中选择一个…编译时是否知道
numArgument
?动态执行此操作是不可能的,因为这将涉及代码生成,但你可以静态生成它或静态生成一组。问题是什么添加操作的pe?@DavideSpataro-否,
numArgument
将在运行时定义。它是否表示
MyMicroservice
的参数数?如果它表示,则在编译时已知。您无法在运行时执行编译时本应执行的操作…
std::make\u index\u sequence
需要参数如果在编译时不知道该值,则注定要创建所有可能支持的选项,然后在运行时从以前预编译的类型中选择一个…我刚刚意识到,即使可以动态构建参数pack从运行时整数开始,
MyMicroservice
参数仍然是静态的。需要为每一组不同的参数定义一个函数。我感谢你的回答,它帮助我发现了我想法中的缺陷。我刚刚意识到,即使我可以从运行时整数动态构建参数包,
MyMicroservice
a参数仍然是静态的。需要为每一组不同的参数定义一个函数。我感谢你的回答,它帮助我发现了我想法中的缺陷。