Hello,
Peter Dimov wrote: It isn't clear why do you need to bind bar2 at all, instead of just using boost::function < void (int) > f3( &bar2 ) ;
To make a long story short, i'd like people always use the same mecanism (syntax), not a boost::bind here and nothing there. Maybe I tried to simplify my problem too much. The small code i'm "working" on is at the end of this message. I will try to say what i really want to do with words. The idea : class FunctionInterface : -> a pure virtual classe with an public void execute(const std::string&) = 0 member class GeneralFunctor : -> a class with a boost::shared_ptr<FunctionInterface> function_ member which is called via the operator()(const std::string) const ; The constructor take a pointer to a FunctionInterface. template < typename Func > class Functor : public FunctionInterface -> keeps a boost::function<Func> as a member and implement the execute function template < typename FuncType > GeneralFunctor MakeFunctor(FuncType func) -> This is my helper function to build a GeneralFunctor from any boost::function Here is some example : GeneralFunctor f1( MakeFunctor( boost::bind(&bar) ) ) ; GeneralFunctor f2( MakeFunctor( boost::bind(&bar2,_1) ) ) ; But i'd like to write for the convenience of the user and save some typing : GeneralFunctor f2( MakeFunctor( boost::bind(&bar2) ) ) ; But why do i need to use boost::bind here ? Why not directy a boost::function like you suggest to me ? Something like GeneralFunctor f3( MakeFunctor( boost::function<void (int)>(&bar2) ) ) ; As i said, i don't want people (me ;)) asking themself what they should do in this or this situation. That's why i introduce my helper function, mybind, which i can use like that : template <typename Ret, typename Arg1> boost::function<Ret (Arg1)> mybind(Ret (*Func)(Arg1)) { boost::function<Ret (Arg1)> f(Func) ; return f ; } GeneralFunctor f4( MakeFunctor( mybind(bar2) ) ) ; I really can always call MakeFunctor with a boost::function directly, something like MakeFunctor( boost::function<void (int)>(&bar2) ) But my purpose is just to make my life easier by using a uniform way letting boost::bind, or a plenty of overloaded mybind function, doing all the work :D I'd like not to fill the boost::function template parameters myself each time. My message is certainly quite confuse, i'm sorry for that but it certainly show that i don't know well the subject ... and the english language =) Thank you anyway. =========================== #include <iostream> #include <string> #include <stdexcept> #include <boost/smart_ptr.hpp> #include <boost/bind.hpp> #include <boost/function.hpp> class FunctionInterface { public: typedef boost::shared_ptr<FunctionInterface> pointer ; virtual ~FunctionInterface() {} virtual std::string execute(const std::string& params="") const = 0 ; } ; template < typename Func > class Functor : public FunctionInterface { private: typedef Func func_type ; func_type func_ ; public: Functor(Func f) : func_(f) {} std::string execute(const std::string& params="") const { return "" ; } } ; class GeneralFunctor { public: GeneralFunctor(const FunctionInterface::pointer func) : function_(func) {} std::string operator ()(const std::string& params = "") const { if ( !function_ ) throw "Error calling null function pointer" ; return function_->execute(params) ; } private: boost::shared_ptr<FunctionInterface> function_ ; } ; template < typename FuncType > GeneralFunctor MakeFunctor(FuncType func) { FunctionInterface::pointer ptr(new Functor<FuncType>(func)) ; return GeneralFunctor(ptr) ; } template <typename Ret, typename Arg1> boost::function<Ret (Arg1)> mybind(Ret (*Func)(Arg1)) { boost::function<Ret (Arg1)> f(Func) ; return f ; } void bar() {} void bar2(int a) {} int main (int argc, char* argv[]) { try { GeneralFunctor f1( MakeFunctor( boost::bind(&bar) ) ) ; GeneralFunctor f2( MakeFunctor( boost::bind(&bar2,_1) ) ) ; GeneralFunctor f3( MakeFunctor( boost::function<void (int)>(&bar2) ) ) ; GeneralFunctor f4( MakeFunctor( mybind(bar2) ) ) ; } catch(std::exception& e) { std::cout << e.what() << '\n' ; return -1 ; } catch(...) { std::cout << "Non standard exception\n" ; return -1 ; } } =========================== Le Fri, 12 May 2006 22:18:57 +0300, Peter Dimov a écrit :
jbd wrote:
template <typename Ret, typename Arg1> boost::function<Ret (Arg1)> mybind(Ret (*Func)(Arg1)) { boost::function<Ret (Arg1)> f(Func) ; return f ; }
[...]
// doesn't work : // boost::function < void (int) > f2( boost::bind(&bar2) ) ;
// this is ok boost::function < void (int) > f3( mybind(&bar2) ) ;
It isn't clear why do you need to bind bar2 at all, instead of just using
boost::function < void (int) > f3( &bar2 ) ;