升压绑定占位符参数等于可变参数模板参数的个数 [英] Boost bind placeholder argument equal to the number of Variadic Template arguments

查看:293
本文介绍了升压绑定占位符参数等于可变参数模板参数的个数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用传递给一个可变参数模板作为一个boost :: bind调用占位符参数的个数。

I want to know if it is possible to use the number of arguments passed to a variadic template as placeholder in a boost::bind call.

事情是这样的:

template <typename ... Args>

boost::bind(&function, this, anArg, _1));         //If Args count equals 1
boost::bind(&function, this, anArg, _1, _2));     //If Args count equals 2
boost::bind(&function, this, anArg, _1, _2, _3)); //If Args count equals 3

这可能吗?

感谢您

推荐答案

有肯定是有部分专精的方法。
你的可变参数不知道参数的个数马上对不对?你必须使用编译时间递归,在这段时间内,你可以使用boost :: MPL栈你的论点(或使用简单的整型常量增量指望他们)。
然后在最后一个非可变参数递归调用(0 ARG)你叫MPL ::大小的容器上(或只使用积分计数器,如果你选择这种方式)来调用一个可调用像其他的答案,那承担所有的参数加在类型列表的开头一个整体模板paramater。那就是你什么专业。您为参数每个数字将根据其专业数量的参数调用正确的绑定呼叫者。
(可调用结构(部分),根据积分参数模板参数的一批专业化,并且即使呼叫函数采用参数的最大数量,它只是包装了正确的boost :: bind调用例如绑定(.., _1,_2)为可赎回2中,T1,T2,T3>)
它并不可怕,但我确认我已经使用在C ++ 03在过去的这个方法。

There definitely is a way with partial specialization. your variadic doesn't know the number of arguments right away right ? you have to use compile-time recursion, during this time you can stack your arguments using boost::mpl (or count them using a simple integral constant increment). then in your last non-variadic recursion call (with 0 arg) you call mpl::size on your container (or just use the integral counter if you chose that way) to call a Callable like the other answer, that bears all the arguments, plus one integral template paramater at the beginning of the type list. and that is what you specialize. you make a caller for each number of arguments that will call the correct bind according to its specialized number of arguments. (the Callable structures are (partially) specialized according to the number of argument integral template parameter. and even though the Call function takes the max number of argument, it only wraps the correct boost::bind call for example the bind(..,_1,_2) for the Callable<2, T1, T2, T3>) its not terrible, but I confirm that I have used this approach in C++03 in the past.

这篇关于升压绑定占位符参数等于可变参数模板参数的个数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆