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 6175253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:53:35+00:00 2026-05-23T23:53:35+00:00

Recently I was trying to create flexible observer pattern implementation which hides boost::signal .

  • 0

Recently I was trying to create flexible observer pattern implementation which hides boost::signal. I almost succeeded.

I have a Observer class which has to have an update method matching signature provided by template parameter.

Example of use:

Observable<void(float, float)> observable;
Observer<void(float, float)> observer;
observable.attach(&observer);
observable.notify(Observable::Arguments(10.0f, 1.0f)); // invokes observer->update(10.0f, 1.0f);

Everything works just fine if observer does not have overloaded update method. In that case boost::bind can not deduce correct method to use. Unfortunately I can’t use explicit casting because I don’t know update arguments (this information is in FunctionSignature).

Following method causes troubles:

class Observable <typename FunctionSignature>
{
...
template <class DerivedObserverClass>
void attach(DerivedObserverClass* observer)
{
    STATIC_ASSERT((boost::is_base_of<ObserverType, DerivedObserverClass>::value));

    ConnectionsMap::iterator it = connections.find(observer);
    if (it == connections.end() || !it->second.connected()) {
        // i would like to do something like 
            // boost::function<FunctionSignature> f;
        // f = boost::bind(&static_cast<FunctionSignature>DerivedObserverClass::update, observer, _1);

        // singnalSlot is defined as boost::signal<FunctionSignature>
        // this works as long, as Derived class doesn't have overloaded update method
        connections[observer] = signalSlot.connect(boost::bind(&DerivedClass::update, observer, _1));
    } else {
        throw std::invalid_argument("Observer already attached.");
    }
}

I think that boost::function could help to solve this problem. I don’t know how to bind it with correct member method using only template signature.

Is it even possible?

  • 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-23T23:53:36+00:00Added an answer on May 23, 2026 at 11:53 pm

    No, boost::function won’t help you either. 13.4.3 says

    Nonstatic member functions match targets of type
    “pointer-to-member-function;” the function type of the pointer to
    member is used to select the member function from the set of
    overloaded member functions.

    This means you cannot take an address of overloaded member function, pass it to any kind of function object (templated or not, boost or std or whatever), and hope the overloading will resolve itself. You need a genuine honest pointer-to-member-function type at the left-hand side of the assignment.

    You will have to convert your FunctionSignature to a pointer-to-member-function type somehow. Here’s some old-fashioned template magic that does what you need, for a limited number of function arguments. c++0x might have a better, more general solution.

    template <typename C, typename F>
    struct tomemfun;
    
    template <typename C, typename res>
    struct tomemfun<C, res()>
    {
      typedef res (C::*memfun_t)();
    };
    
    template <typename C, typename res, typename arg1>
    struct tomemfun<C, res(arg1)>
    {
      typedef res (C::*memfun_t)(arg1);
    };
    
    template <typename C, typename res, typename arg1, typename arg2>
    struct tomemfun<C, res(arg1, arg2)>
    {
      typedef res (C::*memfun_t)(arg1, arg2);
    };
    
    // repeat with more arguments as needed
    

    Now you can use

    tomemfun<DerivedClass, FunctionSignature>::memfun_t update = &DerivedClass::update;
    

    and it will resolve to the right overloaded function.

    boost might already have such a conversion template, but I couldn’t find it.

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

Sidebar

Related Questions

I've recently been trying to create units tests for some legacy code. I've been
I recently came across this in some code - basically someone trying to create
I've been trying to create a transparent table view recently but I'm not having
Recently I have been working with a SQL Server database and I was trying
I am trying to create a WPF based PropertyGrid. Recently i tried wpg.codeplex.com project,
I recently started programming with xCode and PyObjC and I'm trying to create a
I recently installed VS2010 Ultimate. I'm trying to create a MFC dialog based program.
I recently stumbled upon the Object.create() method in JavaScript, and am trying to deduce
I was recently trying to explain to a programmer why, in ASP.Net, they should
Weve recently been trying to work on an application that uses pandastream to encode

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.