我想创建一个std::函数,但增量地添加参数
I want to create a std::function but add args incrementally
我希望有人能帮我一点忙。我对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
"的
相关文章:
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 当从函数参数中的临时值调用复制构造函数时
- 如何从"decltype()"获取函数参数的数量<funtion>?
- 如何将lambda作为模板类的成员函数参数
- 模板参数推导失败,函数参数/参数不匹配
- 如何在C++中将迭代器作为函数参数传递
- 将函数参数"const char*"转换为"std::string_view"是
- C++ 如何将数组值解压缩为函数参数
- 主函数参数的属性
- 具有两个间接寻址运算符 (C++) 的函数参数的用途
- "Warning: Comma within array index expression"但逗号分隔函数参数
- 如何定义在用作函数参数时工作的类模板的转换
- 将函数参数完美转发到函数指针:按值传递呢?
- 为什么我不能将引用作为 std::async 的函数参数传递
- 什么..(省略号)作为函数原型中唯一的函数参数,C++?
- 是否可以就地构造一个固定大小的数组作为函数参数?
- 接受模板作为函数参数
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- Arduino 函数参数