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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:56:34+00:00 2026-05-18T23:56:34+00:00

Say want to store the following: typedef std::function<void(int)> MyFunctionDecl; ..in a collection: typedef std::vector<MyFunctionDecl>

  • 0

Say want to store the following:

typedef std::function<void(int)> MyFunctionDecl;

..in a collection:

typedef std::vector<MyFunctionDecl> FunctionVector;
FunctionVector v;

This is possible, but if I want to find something using std::find:

FunctionVector::const_iterator cit = std::find(v.begin(), v.end(), myFunctionDecl);

.. we get an error due to the == operator.

As has been suggested to me in a previous question concerning this, this can be gotten around by encapsulating the function declaration within another class, which provides a == operator:

class Wrapper
{
private:
    MyFunctionDecl m_Func;

public:
    // ctor omitted for brevity

    bool operator == (const Wrapper& _rhs)
    {
         // are they equal?
    }; // eo ==
};  // eo class Wrapper

So what I want to do is somehow generate a hash for “MyFunctionDecl” so that I can properly implement the == operator. I could have some kind of unique identifier, and ask the caller to provide a unique identifier for the delegate, but this seems a bit of a pain and is error prone.

Is there a way that I can do this? So that the same functions will return the same ID for comparative purposes? So far, the only way around it is to dump the notion of using std::function and go back to using fast delegates which support comparisons. But then I lose the ability to use lambdas.

Any help appreciated!

EDIT

Given the answer below, this is what I have come up with… any caveats I might have missed? I’m in the process of putting it through it’s paces now:

class MORSE_API Event : boost::noncopyable
{
public:
    typedef std::function<void(const EventArgs&)> DelegateType;
    typedef boost::shared_ptr<DelegateType> DelegateDecl;

private:
    typedef std::set<DelegateDecl> DelegateSet;
    typedef DelegateSet::const_iterator DelegateSet_cit;
    DelegateSet m_Delegates;

public:
    Event()
    {
    };  // eo ctor


    Event(Event&& _rhs) : m_Delegates(std::move(_rhs.m_Delegates))
    {
    };  // eo mtor

    ~Event()
    {
    };  // eo dtor

    // methods
    void invoke(const EventArgs& _args)
    {
        std::for_each(m_Delegates.begin(),
                      m_Delegates.end(),
                      [&_args](const DelegateDecl& _decl) { (*_decl)(_args); });
    };  // eo invoke

    DelegateDecl addListener(DelegateType f)
    {
        DelegateDecl ret(new DelegateType(f));
        m_Delegates.insert(ret);
        return ret;
    };  // eo addListener

    void removeListener(const DelegateDecl _decl)
    {
        DelegateSet_cit cit(m_Delegates.find(_decl));
        if(cit != m_Delegates.end())
            m_Delegates.erase(cit);
    };  // eo removeListener

};  // eo class Event
  • 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-18T23:56:35+00:00Added an answer on May 18, 2026 at 11:56 pm

    Have you looked at Boost Signals? It may already be doing what you want to do.

    Anyway, a simple way of wrapping the function would be to use a shared_ptr. If you do

    typedef std::shared_ptr<std::function<void(int)> > MyFunctionDecl;
    

    and make sure that the function is wrapped immediately inside the shared_ptr when you create it (so that the pointer is unique), pointers can be tested for equality so std::find would work.

    For example you can do so with a factory function like

    template <class Functor>
    MyFunctionDecl createDelegate(Functor f) {
        return MyFunctionDecl(new std::function<void(int)>(f));
    }
    

    This way you give an unique identity to the function (its pointer) when you create the delegate.

    BTW, I’d use an std::set instead of an std::vector, as both find and erase are logarithmic rather than linear.

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

Sidebar

Related Questions

I think this should be simple. Say I've got the following jQuery: $someForm.live('submit', function(event)
Let's say I have the following entity: public class Store { public List<Product> Products
Say you want a simple maze on an N by M grid, with one
Say you want to generate a matched list of identifiers and strings enum {
Say I want to copy the contents of a directory excluding files and folders
Say I want to create vb.net application in Visual studio 2005. What is the
Say I want to get the HTML of http://www.google.com as a String using some
Say I want to design a database for a community site with blogs, photos,
Say I want to check if a record in a MySQL table exists. I'd
Lets say you want to create a listing of widgets The Widget Manufacturers all

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.