Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6218077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:32:32+00:00 2026-05-24T07:32:32+00:00

class Foo { double f1( int x, std::string s1 ); double f2( int x,

  • 0
class Foo 
{
    double f1( int x, std::string s1 );
    double f2( int x, SomeClass s2 );
}

I want to be able to bind Foo.f1’s s1 without an instance of foo to create in essense

typedef double (Foo::* MyFooFunc)( int ) 

MyFooFunc func1 = boost::bind( &Foo::f1, _1, _2, "some string" );
MyFooFunc func2 = boost::bind( &Foo::f2, _1, _2, SomeClass );

Then I pass func1 and func2 as parameters to other functions, inside which Foo is finally bound:

void SomeOtherFunction( MyFooFunc func )
{
     Foo foo;
     boost::function< double (int) > finalFunc =
          boost::bind( func, foo, _1 );
}

Questions:
Is this possible? If yes, 1) how to achieve it? 2) What’s the declaration of MyFooFunc?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:32:33+00:00Added an answer on May 24, 2026 at 7:32 am
    typedef double (Foo::* MyFooFunc)( int );
    
    MyFooFunc func1 = boost::bind( &Foo::f1, _1, _2, "some string" );
    

    The result of boost::bind is not a pointer to member, so func1 cannot be initialized as such on the second line. The result of boost::bind is an unspecified type (which will depend on the parameters). If you’re using C++0x, the simplest way to name the result of a call to bind is to use auto:

    auto func1 = boost::bind( &Foo::f1, _1, _2, "some string" );
    

    Another simple way (not restricted to C++03) is simply to not name the result, but to use it on the spot:

    SomeOtherFunction(boost::bind(&Foo::f1, _1, _2, "some string"));
    

    Or, you can use type-erasure to store the result of boost::bind into a boost::function, which you seem to be familiar with. boost::function<double(Foo&, int)> is a possibility but not the only choice.


    We now need to find the appropriate signature for SomeOtherFunction: again, a pointer to member can’t be initialized from the result of a call to boost::bind, so void SomeOtherFunction(MyFooFunc func); won’t work. You can make the function a template instead:

    template<typename Func>
    void SomeOtherFunction( Func func )
    {
         Foo foo;
         boost::function< double (int) > finalFunc =
              boost::bind( func, foo, _1 );
    }
    

    If a template is not preferrable, then you must use some kind of type-erasure such as, again, boost::function.

    void SomeOtherFunction(boost::function<double(Foo&, int)> const& func);
    

    (once again other boost::function types are possible depending on details such as passing a ref-to-const as opposed to a ref-to-non-const)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given: case class Foo(a: Int, b: String, c: Double) you can say: val params
public class Foo implements java.io.Serializable { private int v1; private static double v2; private
Consider the following setup: I am given an interface template<class T> void FooClass<T>::foo(boost::function<double (int)>
class foo { public: void say_type_name() { std::cout << typeid(this).name() << std::endl; } };
class Foo(){ public List<string> SomeCollection; } I need to implement an event which can
class Foo { public: void bar(); }; void Foo::bar() { static int n =
class Foo { public static IEnumerable<int> Range(int start, int end) { return Enumerable.Range(start, end);
class Foo { public: Foo() { Foo(1)} Foo(int x, int y = 0):i(x) {}
class Foo { public int A { get; set; } } class Program {
//Using g++ and ubuntu. #include <vector> using namespace std; Define a class: class foo(){

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.