一个向量/映射中的std :: function变量参数 [英] std::function variable arguments in one vector/map
本文介绍了一个向量/映射中的std :: function变量参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
今天怎么能在c ++中不使用两个单独的持有人呢?
how could one do this in c++ today without using two separate holders?
typedef std::function<void(int a, int b)> f1;
typedef std::function<void(int a)> f2;
std::vector<f1> m;
void add(f1 f)
{
m.push_back(f);
}
void add(f2 f)
{
// add one more (unused) parameter to f2 so we can add f2 to f1 vector holder?
}
我们能否以某种方式重载f1函数以包含不同的参数集?
可以通过如今的可变参数模板或类似的方法来解决吗?
can we somehow overload f1 function to include different set of parameters? could this be solved by variadic templates nowdays or something similar?
推荐答案
创建与新签名匹配的新lambda并
Create a new lambda matching the new signature and add that instead:
void add(f2 f)
{
m.push_back( [g = std::move(f)](int a, int /* unused */){ g(a); } );
}
这篇关于一个向量/映射中的std :: function变量参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文