我想存储一个动作向量,该动作向量可以应用于具有特定签名的任何类(在此测试案例中,将整数和double用作输入)。我已经用std :: function和std :: bind成功地对此进行了编码。
现在,我想创建一个函数,在该函数中我可以将输入传递到并调用push_back。我似乎无法弄清楚如何编写此函数(即,其参数的签名)。通过stackoverflow暂存了一段时间;有人可以帮忙吗?
#include <iostream>
#include <functional>
#include <vector>
struct TestA
{
void regular(int64_t a, double b)
{
std::cout << "TestA::regular(" << a << "," << b << ")" << std::endl;
}
};
struct TestB
{
void alternate(int64_t a, double b)
{
std::cout << "TestB::alternate(" << a << "," << b << ")" << std::endl;
}
};
int main(int argc, char **argv)
{
TestA a;
TestB b;
std::vector< std::function<void(int64_t, double)> > vec;
vec.push_back( std::bind(&TestA::regular, &a, std::placeholders::_1, std::placeholders::_2) );
vec.push_back( std::bind(&TestB::alternate, &b, std::placeholders::_1, std::placeholders::_2) );
// thisIsTheFunctionIWantToWrite(vec, &TestA::regular, &a);
for(auto f: vec)
f(1,2.3);
}
我希望函数thisIsTheFunctionIWantToWrite执行以下操作(尽管我不知道该为HELP添加什么内容):
void thisIsTheFunctionIWantToWrite(std::vector< std::function<void(int64_t, double)> >& vec, HELP x, HELP y){
vec.push_back( std::bind(x, y, std::placeholders::_1, std::placeholders::_2) );
}