Boost logo

Boost Users :

Subject: [Boost-users] [boost.bind] How to retrieve the function type of a boost::bind result
From: Olaf Krzikalla (krzikalla_at_[hidden])
Date: 2009-01-26 07:42:11


Hi,

The following snippet won't compile since the compiler can't deduce the
template argument:

template<typename T>
void foo(boost::function<T> func) { /*...*/ }

void bar() {}

void baz()
{
   foo(boost::bind(&bar)); // error
   foo<void()>(boost::bind(&bar)); // OK
   foo(boost::function<void()>(boost::bind(&bar))); // OK
}

So how do I get the first line of baz to work? That is, is there a
generic way to compute the boost::function type from the bind result?

Background code follows here:

template<typename T>
T getArg(int i) { /*...*/ }

template<typename T>
struct arity_dispatch;

template<>
struct arity_dispatch< boost::function<void()> >
{
   static void call(const boost::function<void()>& f)
     { f(); }
};

template<class T>
struct arity_dispatch< boost::function<void(T)> >
{
   static void call(const boost::function<void(T)>& f)
     { f(getArg<T>(0)); }
};

template<class T, class U>
struct arity_dispatch< boost::function<void(T, U)> >
{
   static void call(const boost::function<void(T, U)>& f)
     { f(getArg<T>(0), getArg<T>(1)); }
};

//aso.

template<typename T>
void generic_call(boost::function<T> func)
{
   arity_dispatch< boost::function<T> >::call(func);
}

Neither is it possible for me to call "generic_call" with a boost::bind
result nor is it possible to (partial) specialize for the _bi::_bind_t
types as these types are internal ones. Also it seems that I can't even
statically compute the arity of a boost::bind result (which would indeed
help a lot).
Any suggestions? If not I propose the introduction of a type
function_type in the return type of a bind expression (I already needed
it once before).

Thanks in advance
Olaf Krzikalla


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net