具有混合引用和非引用类型的可变参数模板

时间:2018-05-19 16:57:31

标签: templates boost-asio variadic-templates

我需要使用variadic模板注册一些函数,比如

std::function<R(Args...)>;

所以我可以注册这样的类成员函数:

std::function<void(std::string)> F1 = [this](std::string msg)->void {Fun1(msg); };
RegisterCmd(1, F1);

std::function<void(int ,double )> F = [this](int p1 ,double p2)->void {Fun2(p1,p2); };
RegisterCmd(2,F2);

和可变参数模板函数如下:

template<typename R,typename... Args> bool RegisterCmd(uint16_t CmdId, std::function <R(Args...)> RealFunc);

但现在我必须通过引用传递一些不可复制的变量,如

boost::asio::ip::tcp::socket socket_

所以我必须将可变参数模板函数更改为

template<typename R,typename... Args> bool RegisterCmd(uint16_t CmdId, std::function <R(Connection & ,Args...)> RealFunc);

但是这会导致模板hpp文件发生很多变化。 有没有办法优雅地解决问题?

0 个答案:

没有答案
相关问题