
Hi, I wish to use boost::bind to simply wrap a member function pointer. I'm passing a member function into a slot in the Boost.Signals library, and I would like the convenience of not having to specify the number of arguments each member function has in boost.bind. For example, assume I have the following class: class Foo { public: void Function1( int ) {} void Function2( int, int, bool ) {} }; I would like to be able to do the following: Foo myFoo; boost::bind( &Foo::Function1, myFoo ); In the case above, I now have an object bound to my function pointer, however I am required to explicitly place a _1, _2, _3 (etc) depending on how many parameters the member function takes. Is there a way that this can be automated? Thanks.

Robert Dailey wrote:
In the case above, I now have an object bound to my function pointer, however I am required to explicitly place a _1, _2, _3 (etc) depending on how many parameters the member function takes. Is there a way that this can be automated? Thanks.
I did something like that using variadic templates (with gcc-4.3 in c++0x mode) for calling member functions without specifying the parameter count. It's something like this: template <class T, class U, class R, class... ARGS> class memfun_caller_t { public: memfun_caller_t(T *ptr, std::function<R(U*,ARGS...)> member) : m_ptr(ptr), m_member(member) {} R operator()(ARGS&&... args) { return m_member(m_ptr, std::forward<ARGS>(args)...); } private: T *m_ptr; std::function<R(U*,ARGS...)> m_member; }; template <class T, class U, class R, class... ARGS> memfun_caller_t<T,U,R,ARGS...> bind(R(T::*member)(ARGS...), U *obj)/*{{{*/ { return memfun_caller_t<T,U,R,ARGS...>(obj, member); } Unfortunately boost::bind didn't help me on this one. If you can't cope with variadic templates, boost::preprocessor could be used to generate overloads. Regards, rod

On Wed, Mar 12, 2008 at 1:45 PM, Robert Dailey <rcdailey@gmail.com> wrote:
Hi,
I wish to use boost::bind to simply wrap a member function pointer. I'm passing a member function into a slot in the Boost.Signals library, and I would like the convenience of not having to specify the number of arguments each member function has in boost.bind. For example, assume I have the following class:
class Foo { public: void Function1( int ) {} void Function2( int, int, bool ) {} };
I would like to be able to do the following:
Foo myFoo; boost::bind( &Foo::Function1, myFoo );
In the case above, I now have an object bound to my function pointer, however I am required to explicitly place a _1, _2, _3 (etc) depending on how many parameters the member function takes. Is there a way that this can be automated? Thanks.
Please see: http://dancinghacker.com/code/dataflow/dataflow/utility/bind_mem_fn.html Regards, Stjepan

On Wed, Mar 12, 2008 at 4:00 PM, Stjepan Rajko <stipe@asu.edu> wrote:
On Wed, Mar 12, 2008 at 1:45 PM, Robert Dailey <rcdailey@gmail.com> wrote:
Hi,
I wish to use boost::bind to simply wrap a member function pointer. I'm passing a member function into a slot in the Boost.Signals library, and I would like the convenience of not having to specify the number of arguments each member function has in boost.bind. For example, assume I have the following class:
class Foo { public: void Function1( int ) {} void Function2( int, int, bool ) {} };
I would like to be able to do the following:
Foo myFoo; boost::bind( &Foo::Function1, myFoo );
In the case above, I now have an object bound to my function pointer, however I am required to explicitly place a _1, _2, _3 (etc) depending on how many parameters the member function takes. Is there a way that this can be automated? Thanks.
Please see:
http://dancinghacker.com/code/dataflow/dataflow/utility/bind_mem_fn.html
This sounds like exactly what I want. However, I am not able to find out what header file bind_mem_fn is. I also do not have a 'dataflow' directory in boost. I'm using boost 1.34.1. Where can I find this? Thanks.

On Wed, Mar 12, 2008 at 3:00 PM, Robert Dailey <rcdailey@gmail.com> wrote:
On Wed, Mar 12, 2008 at 4:00 PM, Stjepan Rajko <stipe@asu.edu> wrote:
Please see:
http://dancinghacker.com/code/dataflow/dataflow/utility/bind_mem_fn.html
This sounds like exactly what I want. However, I am not able to find out what header file bind_mem_fn is. I also do not have a 'dataflow' directory in boost. I'm using boost 1.34.1. Where can I find this? Thanks.
The dataflow library is not in boost yet - I only recently requested a review. I think the following two files should do it (you can get them from the boost sandbox svn): http://svn.boost.org/svn/boost/sandbox/SOC/2007/signals/boost/dataflow/utili... http://svn.boost.org/svn/boost/sandbox/SOC/2007/signals/boost/dataflow/utili... I've only been testing this with boost trunk, on GCC 4.0.1 (OS X) and MSVC8. I think these two files might also work with a release version of boost, but I'm not sure. Please let me know how it goes for you. Thanks, Stjepan

On Wed, Mar 12, 2008 at 5:22 PM, Stjepan Rajko <stipe@asu.edu> wrote:
This sounds like exactly what I want. However, I am not able to find out what header file bind_mem_fn is. I also do not have a 'dataflow'
On Wed, Mar 12, 2008 at 3:00 PM, Robert Dailey <rcdailey@gmail.com> wrote: directory
in boost. I'm using boost 1.34.1. Where can I find this? Thanks.
The dataflow library is not in boost yet - I only recently requested a review.
I think the following two files should do it (you can get them from the boost sandbox svn):
http://svn.boost.org/svn/boost/sandbox/SOC/2007/signals/boost/dataflow/utili...
http://svn.boost.org/svn/boost/sandbox/SOC/2007/signals/boost/dataflow/utili...
I've only been testing this with boost trunk, on GCC 4.0.1 (OS X) and MSVC8. I think these two files might also work with a release version of boost, but I'm not sure. Please let me know how it goes for you.
This will not work because of this line in bind_mem_fn.hpp: #include <boost/function_types/function_arity.hpp> function_types is not in 1.34.1 :(

Hello, From: "Robert Dailey" <rcdailey@gmail.com> To: <boost@lists.boost.org> Sent: Thursday, March 13, 2008 12:16 AM Subject: Re: [boost] [bind] Automatic argument counting
On Wed, Mar 12, 2008 at 5:22 PM, Stjepan Rajko <stipe@asu.edu> wrote:
This sounds like exactly what I want. However, I am not able to find out what header file bind_mem_fn is. I also do not have a 'dataflow'
On Wed, Mar 12, 2008 at 3:00 PM, Robert Dailey <rcdailey@gmail.com> wrote: directory
in boost. I'm using boost 1.34.1. Where can I find this? Thanks.
The dataflow library is not in boost yet - I only recently requested a review.
I think the following two files should do it (you can get them from the boost sandbox svn):
http://svn.boost.org/svn/boost/sandbox/SOC/2007/signals/boost/dataflow/utili...
http://svn.boost.org/svn/boost/sandbox/SOC/2007/signals/boost/dataflow/utili...
I've only been testing this with boost trunk, on GCC 4.0.1 (OS X) and MSVC8. I think these two files might also work with a release version of boost, but I'm not sure. Please let me know how it goes for you.
This will not work because of this line in bind_mem_fn.hpp:
#include <boost/function_types/function_arity.hpp>
function_types is not in 1.34.1 :(
Why you don't take also this file is this could help you ;-)? You can get it from the trunk. ____________________ Vicente Juan Botet Escriba

On Thu, Mar 13, 2008 at 1:03 PM, vicente.botet <vicente.botet@wanadoo.fr> wrote:
This will not work because of this line in bind_mem_fn.hpp:
#include <boost/function_types/function_arity.hpp>
function_types is not in 1.34.1 :(
Why you don't take also this file is this could help you ;-)? You can get it from the trunk.
Because, like with all other boost library components, I'm afraid of cherry picking files because they are usually involved in a large web of dependencies. More than likely I won't be just taking one file, I'll be taking many files and doing a lot of manual hunting. Even if I get all the files, there might also be compatibility issues when taking Boost 1.35 files and placing them in boost 1.34.1. I would rather not have to wait for subtle issues to arrive.

On Thu, Mar 13, 2008 at 1:32 PM, Robert Dailey <rcdailey@gmail.com> wrote:
On Thu, Mar 13, 2008 at 1:03 PM, vicente.botet <vicente.botet@wanadoo.fr> wrote:
This will not work because of this line in bind_mem_fn.hpp:
#include <boost/function_types/function_arity.hpp>
function_types is not in 1.34.1 :(
Why you don't take also this file is this could help you ;-)? You can get it from the trunk.
Because, like with all other boost library components, I'm afraid of cherry picking files because they are usually involved in a large web of dependencies. More than likely I won't be just taking one file, I'll be taking many files and doing a lot of manual hunting. Even if I get all the files, there might also be compatibility issues when taking Boost 1.35files and placing them in boost 1.34.1. I would rather not have to wait for subtle issues to arrive.
I was actually able to get this working, surprisingly enough. I copied over function_types into boost 1.34.1 and at the very least bind_mem_fn seems to work. However, I'm having a bit of an issue with my design that uses bind_mem_fn. Right now I have a class that has a boost::tuple of objects which, on construction, bind a function to a boost::signal. In my constructor, I fill the tuple with various references to member functions of the class initializing the tuple. For example: using boost::dataflow::utility::bind_mem_fn; Rocket_Input::Rocket_Input() : m_subscriptions( bind_mem_fn( &Rocket_Input::HandleKey, this ), bind_mem_fn( &Rocket_Input::HandleMouseMotion, this ), bind_mem_fn( &Rocket_Input::HandleMouseWheel, this ), bind_mem_fn( &Rocket_Input::HandleMouseButton, this ) ) {} While this does compile, it will crash any time that one of the function objects is called because in the initializer list, 'this' references a NULL pointer when the class object was allocated on the heap via a call to 'new'. Is there a way I can initialize my tuple so that 'this' will be a valid pointer? For now, I might just go ahead and make my m_subscriptions tuple a boost::scoped_ptr and initialize it in the constructor body and see if that helps. Thanks.

On Thu, Mar 13, 2008 at 2:11 PM, Robert Dailey <rcdailey@gmail.com> wrote:
I was actually able to get this working, surprisingly enough. I copied over function_types into boost 1.34.1 and at the very least bind_mem_fn seems to work.
However, I'm having a bit of an issue with my design that uses bind_mem_fn. Right now I have a class that has a boost::tuple of objects which, on construction, bind a function to a boost::signal. In my constructor, I fill the tuple with various references to member functions of the class initializing the tuple. For example:
using boost::dataflow::utility::bind_mem_fn;
Rocket_Input::Rocket_Input() : m_subscriptions( bind_mem_fn( &Rocket_Input::HandleKey, this ), bind_mem_fn( &Rocket_Input::HandleMouseMotion, this ), bind_mem_fn( &Rocket_Input::HandleMouseWheel, this ), bind_mem_fn( &Rocket_Input::HandleMouseButton, this ) ) {}
While this does compile, it will crash any time that one of the function objects is called because in the initializer list, 'this' references a NULL pointer when the class object was allocated on the heap via a call to 'new'. Is there a way I can initialize my tuple so that 'this' will be a valid pointer? For now, I might just go ahead and make my m_subscriptions tuple a boost::scoped_ptr and initialize it in the constructor body and see if that helps.
Thanks.
Never mind, I figured it out. Simply dereference 'this' before passing it into bind_mem_fn() and that seems to fix the issue.

On Thu, Mar 13, 2008 at 2:39 AM, Bruno Lalande <bruno.lalande@gmail.com> wrote:
Your bind_mem_fun sounds very useful, not only inside Dataflow. Maybe it could be a good thing to integrate it into Boost.Bind and have Dataflow taking it there for its own use?
Thanks. It maybe could find a home outside the Dataflow library at some point (I certainly don't mind using it from someplace else), but as-is it probably wouldn't fit boost.bind - it returns a boost::function rather than a bind object, and it expects the object to be passed by reference rather than wrapped in a boost::ref or sent as a pointer or by value (that way works better for me, but goes against boost.bind convention). It also has a dependency on function_types... I think it's one of those glue things that doesn't have a clear home :-( But I agree it would be good to find it one that's less specific than the Dataflow lib. Cheers, Stjepan
participants (5)
-
Bruno Lalande
-
Robert Dailey
-
Rodolfo Schulz de Lima
-
Stjepan Rajko
-
vicente.botet