Note that there are some explanatory texts on larger screens.

plurals
  1. PODefine std::hash<std::function>
    primarykey
    data
    text
    <p>I need to create a templated class that can hold pointers to elements of type <code>T</code> and then performs functions on them. The functions will come from different places, so I need a container to store them, so I can call them later. I decided to use an <code>std::unordered_set</code>, because it offers speed and restricts duplication due to it being implemented as a hash table. I have a whole class written, but it doesn't compile due to there not being a hash function defined for my <code>std::function</code> that takes a pointer of type <code>T</code> and returns <code>void</code>. It's easy enough to specify it with <code>struct hash&lt;std::function&lt;void(MyCustomType*)&gt;&gt;</code> (and overloading the <code>()</code> operator, too) for each type I use, but how do I actually hash the function?</p> <p>Here is a watered-down excerpt from my class with the relevant members and methods:</p> <pre><code>template &lt;typename T&gt; class Master { private: std::unordered_set&lt;std::function&lt;void(T*)&gt;&gt; functions; protected: registerFunction(std::function&lt;void(T*)&gt; function) { this-&gt;functions.insert(function); } unregisterFunction(std::function&lt;void(T*)&gt; function) { this-&gt;functions.erase(function); } }; </code></pre> <p>I'm not completely bound to using an <code>std::unordered_set</code>, but it seems to offer everything that I'd need to get this piece (and the rest of my code) working well.</p> <p>Am I thinking about this the wrong way? Is it completely impossible to hash a <code>std::function</code>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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