Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to overload on std::function signature in C++
    text
    copied!<p>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.</p> <p>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:</p> <pre><code>void operator+=(void (*handler)()); void operator+=(function&lt;void (void *, T)&gt; handler); </code></pre> <p>Usage:</p> <pre><code>MyClass a; a += [](){ DoSomething(); }; a += [](void *x, T y){ DoSomething(); }; </code></pre> <p>Unfortunately, the code does not compile because the compiler cannot determine that the second overload is unsuitable for the first += call.</p> <p>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.</p> <p>In addition, it would be handy to have the following overload as well:</p> <pre><code>void operator+=(function&lt;void()&gt; handler); </code></pre> <p>But again, I can't overload based on the signature of the function&lt;> template.</p> <p>See this thread for further examples: <a href="https://stackoverflow.com/questions/5931214/isnt-the-template-argument-the-signature-of-stdfunction-part-of-its-type">Isn&#39;t the template argument (the signature) of std::function part of its type?</a> (I tried implementing the various solutions mentioned in that thread, and <strong>none of them would compile</strong>)</p> <p>I've been programming for many years in a number of languages, but my C++ skills are a little rusty.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload