我想创建一个std::函数,但增量地添加参数

I want to create a std::function but add args incrementally

本文关键字:函数 参数 添加 std 创建 一个      更新时间:2023-10-16

我希望有人能帮我一点忙。我对c++比较陌生,对模板的概念也比较陌生。我需要创建一个std::函数基于一些数据,我得到一个列表。函数的签名应根据可用的数据进行。我在找这样的东西

template <typename ret, typename... Args, typename newArg>
struct typeparser<ret(...Args)>{
    typeparser<ret(...Args)> insertArg(newArg)
    { 
       retrun typeparser <ret(...args, newArg) > ; 
    }
};

我想做的是迭代boost::variant的向量,然后根据我看到的值的类型,将其添加到参数列表中,一旦完成,创建std:function并从库加载它,然后执行它。明白了吗?

std::vector<boost::varient<int, char, std::string>> list;
arglist = typeparser<int()>;   //all functions have int return, so start with return int and 0 args
for(boost::varient<int, char, std::string> a : list) {
    if(a.type() ==  typeid(int)){
        arglist.addArg(int); // now add int to list of args
    } else 
    if(a.type()== typeid(char)) {
       arglist.add(char);
    } else
    if (a.type()== typeid(bla)) {
        arglist.add(bla);
    }
  } // end for 
//now create the function here 
std::function<arglist> f = //load from library;

这看起来可能吗?也许我看问题的角度不对?这个时候什么都可以。非常感谢!!

std::function必须在编译时指定其所有参数-您所要求的将要求直到运行时才知道参数集,这是不允许的。

理论上可以通过std::function这样包含一堆参数调用或类似的东西来实现你所提议的,但我不相信有一种可移植的方法来实现它。

听起来你最好问一个问题的解决方案,你需要这个"运行时std::function"的