callback and boost:function

Discussion in 'C++' started by Lars Schouw, Jan 11, 2006.

  1. Lars Schouw

    Lars Schouw Guest

    Guys,

    Is there anyway in C++ to setup a callback for a member function that
    takes two arguments
    I am trying to use boost::function but that only works with
    std::bind1st that does only take one parameter.

    My function I want to call back looks like this:
    struct X {
    double f_cos(double x , void * p);
    };

    Regards
    Lars
     
    Lars Schouw, Jan 11, 2006
    #1
    1. Advertising

  2. Lars Schouw

    Lars Schouw Guest

    To get ride of the void* I would like to use the boost::lambda library
    so I can do _1, _2 as arguments.
    But this does also not seems to work with more than one argument.
    Lars
     
    Lars Schouw, Jan 11, 2006
    #2
    1. Advertising

  3. Lars Schouw

    Lars Schouw Guest

    I will have a look at the sigc++ library for now as a workaround until
    someone comes up with a better solution.
    http://libsigc.sourceforge.net
    Lars
     
    Lars Schouw, Jan 11, 2006
    #3
  4. Lars Schouw

    David Harmon Guest

    On 11 Jan 2006 00:49:43 -0800 in comp.lang.c++, "Lars Schouw"
    <> wrote,
    >Is there anyway in C++ to setup a callback for a member function that
    >takes two arguments


    Why are you trying to setup a callback as a member function?

    I suspect you need to read topic "[33.2] How do I pass a
    pointer-to-member-function to a signal handler, X event callback,
    system call that starts a thread/task, etc?" in Marshall Cline's C++
    FAQ. It is always good to check the FAQ before posting. You can
    get the FAQ at:
    http://www.parashift.com/c -faq-lite/
     
    David Harmon, Jan 12, 2006
    #4
  5. Lars Schouw

    Lars Schouw Guest

    David

    I already had a look at the FAQ last time I looked into this problem
    one year ago.

    The FAQ does not describe how to extended it with generic lambda code.
    This is an extra plus I would like to be able to have if possible.

    Last time I looked at it I got it up and running as described in the
    FAQ I will see if I can get it running again.
    boost::function is more elegant so it would be nice if I would be able
    to make that work somehow.
    I am stuck in how to do that at the moment.

    Lars
     
    Lars Schouw, Jan 12, 2006
    #5
  6. Lars Schouw

    Jeff Flinn Guest

    Lars Schouw wrote:
    > Guys,
    >
    > Is there anyway in C++ to setup a callback for a member function that
    > takes two arguments
    > I am trying to use boost::function but that only works with
    > std::bind1st that does only take one parameter.


    I don't know how you came to this conclusion, but this is false.

    > My function I want to call back looks like this:
    > struct X {
    > double f_cos(double x , void * p);
    > };


    Assuming you want to supply an X pointer and both args you could:

    typedef boost::function< double, X*, double, void* > tFnc;

    tFnc lFnc = boost::bind( &X::f_cos, _1, _2, _3 );

    then call via

    lFnc( &x, somedbl, someptr );

    Jeff Flinn
     
    Jeff Flinn, Jan 12, 2006
    #6
  7. Lars Schouw

    Lars Schouw Guest

    typedef boost::function< double, X*, double, void* > tFnc;

    give me:
    c:\sletmig\templatedcallback\test.cpp(18) : error C2977:
    'boost::function' : too many template arguments
    c:\dev\boost\boost_1_33_1\boost\function\function_base.hpp(92)
    : see declaration of 'boost::function'

    when I compile.... any idea?

    Lars
     
    Lars Schouw, Jan 13, 2006
    #7
  8. On 12 Jan 2006 16:46:45 -0800
    "Lars Schouw" <> wrote:

    > typedef boost::function< double, X*, double, void* > tFnc;
    >
    > give me:
    > c:\sletmig\templatedcallback\test.cpp(18) : error C2977:
    > 'boost::function' : too many template arguments
    > c:\dev\boost\boost_1_33_1\boost\function\function_base.hpp(92)
    > : see declaration of 'boost::function'
    >
    > when I compile.... any idea?
    >
    > Lars
    >


    That's because it's a mix between two notations. Thus you write either :

    typedef boost::function< double (X*, double, void*) > tFnc;

    but it does not work with all compiler (look at the boost::function
    websites to know which compilers ...), or you use :

    typedef boost::function3<double, X*, double, void*> tFct;

    Now, if you want to store the function just use :

    tFct fct = &X::f_cos; // No need to use bind !

    However, if you want to use a zero argument function as a callback,
    use :

    boost::function<double> fct = boost::bind(&X::f_cox, x, somedbl,
    someptr);

    Then call :

    double d = fct();

    Also, you can bind part of the arguments using _1, _2, ... and the
    correct function definition.

    Pierre



    --
    You will have good luck and overcome many hardships.
     
    Barbier de Reuille Pierre, Jan 13, 2006
    #8
  9. Lars Schouw

    Lars Schouw Guest

    Pierre

    Is it possible to use Lambda to make struct X generic?
    aka.
    typedef boost::function2<double, GENERIC_TYPE*, double> tFct;

    so that I don't have to know the specification of struct X?

    Regards
    Lars Schouw
     
    Lars Schouw, Jan 24, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Toby Bradshaw
    Replies:
    6
    Views:
    1,820
    Kai-Uwe Bux
    Jun 2, 2006
  2. Misiu
    Replies:
    3
    Views:
    2,456
    Misiu
    Jan 31, 2007
  3. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    The boost.variant library and boost::make_recursive_variant

    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 5, 2007, in forum: C++
    Replies:
    1
    Views:
    733
    dasjotre
    Jul 6, 2007
  4. Barry
    Replies:
    1
    Views:
    483
    Eric Niebler
    Jul 11, 2007
  5. Christopher
    Replies:
    1
    Views:
    865
    Yakov Gerlovin
    Oct 5, 2011
Loading...

Share This Page