How to pass result of boost::bind to arg of template function ?
Hi, I want to pass the result of a boost::bind as an argument of a templated function of a class, and am struggling to find a signature that will let me do this. Is this possible? if so what signature should the function have ? Any help much apreciated Hugo #include "boost/function/function1.hpp" #include "boost/bind.hpp" class another_class {}; class my_class { public: typedef another_class ObjectType; //! this works fine passing a boost::function object template <class ReturnType> void add(const boost::function1<ReturnType,ObjectType>& arg) { boost::function1<ReturnType,ObjectType> fn_obj=arg; } //! this should work when passed the result of a boost::bind template <class ReturnType> void add(?what goes here? & arg) { function1<ReturnType,ObjectType> fn_obj(arg); } }; int fn(another_class,int) { return 1; } int main(int, char**) { my_class c; c.add(boost::bind(&fn,_1,1)); }
On Thursday 31 October 2002 01:22 pm, hugo duncan wrote:
Is this possible? if so what signature should the function have ? //! this works fine passing a boost::function object template <class ReturnType> void add(const boost::function1<ReturnType,ObjectType>& arg) { boost::function1<ReturnType,ObjectType> fn_obj=arg; }
Generally speaking, it's not useful to have any of the template arguments to a Boost.Function object deduced, because it won't work the way you want it to.
//! this should work when passed the result of a boost::bind template <class ReturnType> void add(?what goes here? & arg) { function1<ReturnType,ObjectType> fn_obj(arg); } };
You could take the function object as a template paramter and extract its result_type: template<typename F> void add(F arg) { typedef typename F::result_type ReturnType; function1<ReturnType,ObjectType> fn_obj(arg); } Doug
participants (2)
-
Douglas Gregor
-
hugo duncan