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

  • SEARCH
  • Home
  • 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 1030721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:44:44+00:00 2026-05-16T12:44:44+00:00

I have a class with an Attach function that accepts a function object and

  • 0

I have a class with an “Attach” function that accepts a function object and stores it into a collection. The class itself is templated on the function signature. Something like this:

template<class Signature>
class Event
{
public:

 void Attach(boost::function<Signature> signature)
 {
  MySignatures.push_back(signature);
 }

private:

 std::list<boost::function<Signature>> MySignatures;
};

To demonstrate usage, consider the following class:


class Listening
{
public:

 int SomeFunction(int x, int y, int z); 
};

To pass the function on Listening into Event, I would need to write:


 Event<int(int, int, int)> myEvent;
 Listening myListening;

 myEvent.Attach(boost::bind(boost::mem_fn(&Listening::SomeFunction), &myListening, _1, _2, _3));

So instead of doing so for each case which may be prone to error, I write a set of macros, as follows:


 #define EventArgument0(x, y)  boost::bind(boost::mem_fn(x), y)
 #define EventArgument1(x, y)  boost::bind(boost::mem_fn(x), y, _1)
 #define EventArgument2(x, y)  boost::bind(boost::mem_fn(x), y, _1, _2)
 #define EventArgument3(x, y)  boost::bind(boost::mem_fn(x), y, _1, _2, _3)
 #define EventArgument4(x, y)  boost::bind(boost::mem_fn(x), y, _1, _2, _3, _4)

 etc.

and then I can write:


 myEvent.Attach(EventArgument3(&Listening::SomeFunction, &myListening));

which is much easier to read (I think). Now to my question: how can I instead write:


 myEvent.Attach(EventArgument(&Listening::SomeFunction, &MyListening));

or even better:


 myEvent.Attach(&Listening::SomeFunction, &myListening);

, such that the event Attach will magically bind correctly with the appropriate number of arguments as contained in <Signature> (in this example, int(int, int, int))? I’m open to any template meta-programming magic you have in mind here.

Thanks.

Edit: it turns out I don’t need boost::mem_fn here, because boost::bind is equivalent, so in my macro I can use:

bind(&MyClass::Hello, myClass, _1, _2, _3);

,instead of:

bind(mem_fn(&MyClass::Hello), myClass, _1, _2, _3);

The question remains however: how to pass &MyClass::Hello to the event class and use template overloading to handle the _1, _2, _3, etc. implied by the function prototype used to template the Event class?

  • 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-16T12:44:44+00:00Added an answer on May 16, 2026 at 12:44 pm

    Overload Attach for different numbers of parameters in the member function:

    template<typename R,typename T,typename U>
    void Attach(R (T::*pmf)(),U* p))
    {
        Attach(boost::bind(pmf,p));
    }
    
    template<typename R,typename T,typename U,typename A1>
    void Attach(R (T::*pmf)(A1),U* p))
    {
        Attach(boost::bind(pmf,p,_1));
    }
    
    template<typename R,typename T,typename U,typename A1,typename A2>
    void Attach(R (T::*pmf)(A1,A2),U* p))
    {
        Attach(boost::bind(pmf,p,_1,_2));
    }
    

    If you need to handle const member functions too then you’ll need a second set of overloads.

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

Sidebar

Related Questions

I have a class (or function-containing object; I've heard that there is no such
I have written an apex class that will create a PDF quote and attach
I have a clickable marker that I want to attach either a 'class' or
I have a function that creates a span element with a removeme class and
I have one PHP5 object passing messages to another, and would like to attach
I have a class that has a create method, passing into it a POCO
I have following situation. In a constructor of a pseudo class I attach a
I have Class and Student objects. Both have collection of another as property. Which
I have class that extend FragmentActivity in it I add fragment to layout as
I have class grades that I need to check if a specific grade is

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.