Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost bind placeholder argument equal to the number of Variadic Template arguments

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.

Something like this:

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

Is this possible?

Thank you

like image 441
Blizter Avatar asked Nov 09 '11 01:11

Blizter


1 Answers

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.

like image 196
Lightness1024 Avatar answered Oct 02 '22 03:10

Lightness1024