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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:04:27+00:00 2026-05-23T13:04:27+00:00

I was trying to understand c++0x std::bind and std::function usage in event handling /

  • 0

I was trying to understand c++0x std::bind and std::function usage in event handling / callbacks. So I was examining some code snippets and encountered some interesting places which I can’t fully understand.

Let’s say we have:

class EventHandler
{
public:
    template<typename Event, typename Listener>
    bool bindEvent (const Listener& function)
    {
        if (std::is_array<Event>::value)
        {
            std::cout << "You cannot register an array as an event type" << std::endl;
            return false;
        }
        if (std::is_convertible<Listener, std::function<void (Event&)>>::value)
        {
            std::cout << "Invalid callback for this event type" << std::endl;
            return false;
        }

        listeners.insert(std::make_pair(&typeid(typename std::decay<Event>::type),
                           [=](void* ev) { function(*static_cast<Event*>(ev)); }));
    }
private:
    // for each event type, we have a list of callbacks
    // these callbacks are of the form std::function<void (void*)>
    // because we will convert between the &event and void*
    typedef std::function <void (void*)> fun;
    std::unordered_multimap <const std::type_info*, fun> listeners;
};

// example of an event type
struct KeyboardEvent
{
    int keyCode;
};

// example of an event callback
void do_key (const KeyboardEvent &event)
{
    std::cout << "I am working" << std::endl;
}

int main (int argc, char** argv)
{
    EventHandler handler;
    handler.bindEvent<KeyboardEvent> (&do_key);   // fixed typo

    return 0;
}

Question: What type does Listener holds in this part ?:

template<typename Event, typename Listener>
bool bindEvent(const Listener& function)

Since in main method we call this function only with .

PS: Also, this code fails in std::is_convertible part. (as I understand, because of mismatch type from habindEvent<KeyboardEvent> (&do_key);

  • 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-23T13:04:28+00:00Added an answer on May 23, 2026 at 1:04 pm

    Listener will be inferred by the compiler to be the type of function pointer that you pass it, in this case void(const KeyboardEvent&).

    And your test fails because it’s the wrong way around: you want

    if (!std::is_convertible<Listener, std::function<void (Event&)>>::value)
    

    instead (note the negation).

    By the way, both std::is_array and std::is_convertable are decided at compile time, which means that you are using a run-time check for something that is statically determined. Instead, you can make the template fail to bind for invalid types using SFINAE:

    template<typename Event, typename Listener>
    typename std::enable_if<!std::is_array<Event>::value && std::is_convertible<Listener, std::function<void(Event&)>>::value, bool>::type bindEvent (const Listener& function)
    {
    }
    

    This will cause a compiler error if you try to instantiate the template with types that don’t match your conditions.

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

Sidebar

Related Questions

Trying to understand Ruby a bit better, I ran into this code surfing the
After trying to understand why client code is not rendered in a page (injected
I'm trying to understand someone else's Perl code without knowing much Perl myself. I
One of my personal experiments to understand some of the C++0x features: I'm trying
I'm trying to figure out if I understand correctly what modular code really is.
I'm trying to understand what const_iterator means. I have the following example code: void
I am trying to understand how Boost memory mapped files work. The following code
I am trying to understand a code, here is fragment which is causing confusion:
I am new to boost and trying to write some simple programs to understand
I'm trying to convert some VC++ 6 code to a console app using only

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.