Steve Lorimer wrote:
Hi
Is it possible to pass variadic template arguments to bind, such that bind
uses them as the placeholder arguments (_1, _2, _3 etc)
I show my use case below.
...
template struct cb { typedef void
(T::*type)(Args...); };
...
job_t(typename cb::type cb, boost::shared_ptr<T> that,
boost::shared_ptr q) :
arg_func(boost::bind(cb, that, Args...)), queue(q) {} // normally
would be _1, _2, _3, etc
An interesting puzzle. I tried to apply the 'Indexes' technique shown in the
bind implementation section of
http://osl.iu.edu/~dgregor/cpp/variadic-templates.pdf
and ended up with the following:
#include
#include
template struct int_tuple {};
// make indexes impl is a helper for make indexes
template
struct make_indexes_impl;
template
struct make_indexes_impl, T, Types...>
{
typedef typename make_indexes_impl,
Types...>::type type;
};
template
struct make_indexes_impl >
{
typedef int_tuple type;
};
template
struct make_indexes: make_indexes_impl<0, int_tuple<>, Types...> { };
template< class T, class... Args, int... Indexes >
boost::function< void(Args...) > make_func_helper( void (T::* pm) (Args...),
T * that, int_tuple< Indexes... > )
{
return boost::bind( pm, that, boost::arg()... );
}
template< class T, class... Args >
boost::function< void(Args...) > make_func( void (T::* pm) (Args...), T *
that )
{
return make_func_helper( pm, that, typename
make_indexes::type() );
}
struct X
{
void f( int, int )
{
}
};
int main()
{
X x;
boost::function f = make_func( &X::f, &x );
f( 1, 2 );
}
Hope this helps.