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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:45:02+00:00 2026-06-03T23:45:02+00:00

I want to code a minimal multicast delegate class. Its interface consists of the

  • 0

I want to code a minimal multicast delegate class. Its interface consists of the three operators operator() for calling the delegate and operator+=/operator-= for adding/removing callable types like functions, methods, lambdas or functors.

I have trouble to implement operator-= where I need to compare addresses of function targets for removal. Here is what I came up with so far (I use g++ 4.6.3):

#include <functional>
#include <list>

template <typename ... EventArgs>
class Event
{
public:
    typedef std::function<void (EventArgs...)> EventHandler;

    Event() = default;
    ~Event() = default;
    Event(const Event &) = delete;
    Event & operator=(const Event &) = delete;

    void operator()(EventArgs... eventArgs)
    {
        for (auto eventHandler : m_eventHandlers)
            eventHandler(eventArgs...);
    }

    Event & operator+=(const EventHandler &eventHandler)
    {
        m_eventHandlers.push_back(eventHandler);
        return *this;
    }

    Event & operator-=(const EventHandler &eventHandler)
    {
        m_eventHandlers.remove_if([&](const EventHandler &_eventHandler)
        {
            return false; // TODO: Compare event handlers
        });

        return *this;
    }

private:
    std::list<EventHandler> m_eventHandlers;
};

operator-= works for functions when I substitute the TODO line with the following code:

return * eventHandler.template target<void (*)(EventArgs...)>() ==
       *_eventHandler.template target<void (*)(EventArgs...)>();

Here is a minimal example:

void eventHandler() { /* ... */ }

Event<> event;
event += eventHandler;
event();
event -= eventHandler; // works

However, for other callable types it crashes since std::function.target() returns null pointers. Obviously the template doesn’t fit anymore so I tried to template Event::operator-= but it does not compile for the most callable types due to “incomplete types”:

template<typename T>
Event & operator-=(const T &eventHandler)
{
    m_eventHandlers.remove_if([&](const EventHandler &_eventHandler)
    {
        if (_eventHandler.template target<T>() != null_ptr) // <- ERROR
        {
            // ...
        }
    });

    return *this;
}    

I also tried T* as template parameter for target() but I’m stuck. Is it possible to code a generic operator-= that can correctly compare any callable type?

Thanks in advance!

  • 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-06-03T23:45:03+00:00Added an answer on June 3, 2026 at 11:45 pm

    The problem is that std::function<> doesn’t support comparison operators == and !=. boost::function has an explanation for that.

    One solution is to store the functions in a map or a hash keyed by an integer. Registration would return that integer key. To unregister the key has to be passed, e.g.:

    typedef int handler_id_t;
    handler_id_t add(const EventHandler &eventHandler);
    void del(handler_id_t);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I boiled the thing I want to do to the following minimal code: public
I want to give a minimal js code to random websites so that they
I want code to run whenever I create a new object. For example, see
i want code to check whether the user in logged in or not. I
I want code for keeping 1 second delay in Flex. i.e. i want to
Question: I want code for: syntax highlighting (of programming languages) Language: C# or assembly
Want to code a key pad for an calculator. What I want to make
I want to code a simple form layout in flex. Something like the following:
I want a code that when applied to text in Visual Basic 2008 it
I want some code to be triggered every second. Usually, I'd create a Timer

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.