Binding to a static member function?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Binding to a static member function?

Stephen Gross
I'm trying to figure out the correct syntax for boost::bind-ing a static
member function with some arguments preset. For example:

============================
struct Foo
{
  static double go(int x, int y) { return x + y; }
}

typedef ... functortype;

functortype f  = boost::bind(&Foo::go, _1, boost::constant(3));

f(3);
============================

The actual code I have is a little different; I've simplified it as the
above example. At compile time, the compiler complains that it can't find a
matching call for 'f(3)'. Any ideas? I can post the complete example,
obviously, if that's necessary. Is my syntax for the bind statement correct?

Thanks in advance,
--Steve

Stephen Gross
Case Western School of Medicine
Cleveland, OH

"By Grabthar's hammer, by the sons of Worvan, you shall be avenged." - Dr.
Lazarus


_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Binding to a static member function?

Stuart Dootson
On 2/3/06, Stephen Gross <[hidden email]> wrote:

> I'm trying to figure out the correct syntax for boost::bind-ing a static
> member function with some arguments preset. For example:
>
> ============================
> struct Foo
> {
>   static double go(int x, int y) { return x + y; }
> }
>
> typedef ... functortype;
>
> functortype f  = boost::bind(&Foo::go, _1, boost::constant(3));
>
> f(3);
> ============================
>
> The actual code I have is a little different; I've simplified it as the
> above example. At compile time, the compiler complains that it can't find a
> matching call for 'f(3)'. Any ideas? I can post the complete example,
> obviously, if that's necessary. Is my syntax for the bind statement correct?
>
> Thanks in advance,
> --Steve

1. How do you deduce functortype?
2. Something like this (i.e. using Boost.Function to hold the bound
function) is probably easiest (and compiles and links and runs - I've
checked):

typedef boost::function<double(int)> functortype;
functortype f = boost::bind(&Foo::go, _1, 3);

HTH

Stuart Dootson

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Binding to a static member function?

raulh39@ya.com
In reply to this post by Stephen Gross

Stephen Gross wrote:

>I'm trying to figure out the correct syntax for
boost::bind-ing a static
>member function with some arguments preset. ...
>  
>
This code works for me:

#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <iostream>
struct Foo
{
   static double go(int x, int y) { return x + y; }
};

int main()
{
   typedef boost::function<double(int)> functortype;
   functortype f  = boost::bind(&Foo::go, _1, 4);
   std::cout << f(3) << std::endl;
}

Hope it helps.


 


_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users