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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:42:05+00:00 2026-06-15T17:42:05+00:00

I know this question has been asked before, but despite being a fairly experienced

  • 0

I know this question has been asked before, but despite being a fairly experienced coder I don’t understand the answers and see no way to respond to these previous questions to ask for clarification. There is no “reply” link or anything. Besides, those questions were quite old. So, I’m asking the question afresh.

I have a class, in which I am overloading the += operator. I want one overload to take a bare function pointer, and the other to take a std::function:

void operator+=(void (*handler)());
void operator+=(function<void (void *, T)> handler);

Usage:

MyClass a;
a += [](){ DoSomething(); };
a += [](void *x, T y){ DoSomething(); };

Unfortunately, the code does not compile because the compiler cannot determine that the second overload is unsuitable for the first += call.

How do I define my operator+= member functions to correct this problem? I don’t want to change how the operators are used (e.g. by using an explicit cast). I want them to work as demonstrated above.

In addition, it would be handy to have the following overload as well:

void operator+=(function<void()> handler);

But again, I can’t overload based on the signature of the function<> template.

See this thread for further examples: Isn't the template argument (the signature) of std::function part of its type?
(I tried implementing the various solutions mentioned in that thread, and none of them would compile)

I’ve been programming for many years in a number of languages, but my C++ skills are a little rusty.

  • 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-15T17:42:05+00:00Added an answer on June 15, 2026 at 5:42 pm

    Following code works well with g++ 4.7.2:

    #include <functional>
    #include <iostream>
    
    template <typename T>
    typename std::enable_if<std::is_convertible<T, void(*)()>::value>::type
    foo(T&&)
    {
        std::cout << "foo(void(*)())" << std::endl;
    }
    
    void foo(std::function<void(void*,int)>)
    {
        std::cout << "foo(std::function<void(void*,int)>)" << std::endl;
    }
    
    int main()
    {
        foo([]{});
        foo([](void*,int){});
    }
    

    The problem is caused by the constructor of std::function, which is declared as template <class F> function(F);. That constructor accepts everything as its argument. If that argument doesn’t have an appropriate operator(), an error is generated during the instantiation of the constructor.

    Unfortunately the overload resolution process doesn’t require any template functions to be instantiated, so the compiler believes everything can be converted to any std::function type.

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

Sidebar

Related Questions

I know this question has been asked here before, but I don't think those
I know this question has been asked before, but many answers don't give clear
I don't know if this question has been asked before, but I'll go through
I know this question has been asked before numorous times but they seem to
I know this question has been asked before, but non of the solutions solve
I know this question has been asked before, but I haven't been able to
Yes, I know this question has been asked before, but I can't find an
Alright, I know this question has been asked before, but mine has a different
I know this type of question has been asked and answered before but I
I know that this sort of question has been asked here before, but still

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.