c++ - 动态创建 std::tuple 并将其扩展到参数包中

标签 c++ c++11 variadic

我正在为用 C++ 编写的软件编写插件,这里是定义插件的片段:

extern "C"
irods::ms_table_entry* plugin_factory() {

    // The number of msParam_t* arguments that it will accept
    int numArguments = 2;

    irods::ms_table_entry* msvc = new irods::ms_table_entry(numArguments); 

    msvc->add_operation(
        "my_microservice",
        std::function<int(msParam_t*, msParam_t*, ruleExecInfo_t*)>(MyMicroservice)
    ); 

    return msvc;
}  

我希望能够使用 numArguments动态生成 std::function<int(msParam_t*, msParam_t*, ruleExecInfo_t*)>参数包。在哪里numArguments代表msParam_t*的个数参数。

我不是 C++ 专家(尤其是模板方面),所以 after some research我发现这可能可以通过实现以下内容实现:

  • 标准::元组
  • std::tuple_cat
  • std::index_sequence
  • std::make_integer_sequence

但我真的不知道如何开始实现它。我找到的例子很难理解,我无法将它们转化为我自己的需求。任何人都可以提供有关这可能如何工作的提示、简短示例或引用吗?非常感谢任何信息!

最佳答案

我不知道以下内容是否正是您要问的,但我认为您想要的是能够根据您的 MyMicroservice 使用的参数数量存储在 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;
}

关于c++ - 动态创建 std::tuple 并将其扩展到参数包中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44394094/

相关文章:

C++ 将 LPSTR 缓冲区添加到 char 数组

c++ - 如何将类型标记的标记转换为解析树?

c++11 结合 std::tuple 和 std::tie 以实现高效排序

c++ - 在可变参数构造函数中初始化 const 数组

c++ - 将 vector 传递给某些需要某种大小的数组引用的遗留 API 的任何方法

c++ - 是否有用于阻塞 boost::asio TCP 连接的 boost::iostreams(双向)设备?

c++ - 带有字符的奇怪指针行为

c++ - 将实例数组实现为不同类型的好方法是什么

c++ - 如何将可变函数参数传递给 C++ 中的另一个函数(特别是 std::format)?

C++ : create custom function dispatcher from variadic template