I am new in cpp, I want to pass parameters in a flexible way like this:
#include <iostream>
#include <vector>
struct Req
{
void test()
{ std::cout << "req" << std::endl; }
};
struct Resp
{
void test()
{ std::cout << "resp" << std::endl; }
};
template<typename Fn>
auto update(Fn &&fn_)
-> decltype(fn_(nullptr, nullptr), void())
{
Req req;
Resp resp;
fn_(&req, &resp);
}
template<typename Fn>
auto update(Fn &&fn_)
-> decltype(fn_(nullptr), void())
{
update([&fn_](Req *req, Resp *resp)
{
fn_(resp);
});
}
template<typename Fn>
auto update(Fn &&fn_)
-> decltype(fn_(), void())
{
update([&fn_](Req *req, Resp *resp)
{ fn_(); });
}
int main()
{
update([](Req *req, Resp *resp)
{
std::cout << "1" << std::endl;
req->test();
resp->test();
});
update([](Resp *resp)
{
std::cout << "2" << std::endl;
});
update([]()
{
std::cout << "3" << std::endl;
});
}
But I want to add more parameters. Here I write it in a fixed way.
template<typename Fn>
auto update(Fn &&fn_)
-> decltype(fn_(nullptr, nullptr, 0, 0), void())
{
Req req;
Resp resp;
fn_(&req, &resp, 1, 2);
}
// ...
update([](Req *req, Resp *resp, int i, int j)
{
std::cout << i << " : " << j << std::endl;
req->test();
resp->test();
});
I want to write like this
[](int i, double j, float k ....)
I want to add some other parameters.
How can I use template magic to do it in a flexible way?
Thanks for any help !!!
I want to use c++11 to implement this.
Aucun commentaire:
Enregistrer un commentaire