I have a templated class Timer
template<typename Clock, typename Duration>
class Timer
{
public:
void Register(const std::function<void(const std::chrono::time_point<Clock> &timePoint)> &eventHandler);
private:
std::vector<std::function<void(const std::chrono::time_point<Clock> &timePoint)>> eventHandlers;
// Some more stuff.
};
Now I would like to find duplicate entries in Register
and that's the problem.
I tried different approaches using std::find
and a simple for
loop.
The best I could get so far (removed namespaces for readability):
typename vector<function<void(const time_point<Clock> &timePoint)>>::iterator it;
for(it = eventHandlers.begin(); it != eventHandlers.end(); ++it)
{
}
My problem is to compare the parameter of Register
with it
.
I tried several things like:
typename function<void(const time_point<Clock> &timePoint)> &f = &(*it);
if(f == eventHandler)
{
// Duplicate
}
... and some other more complicated approaches but none of them worked.
I have some trouble solving this as I usually code in C# and therefor I'm not used to do templating in C++.
How can I solve this and are there even more elegant solutions to this problem?
Edit:
I usually get this error:
binary '==': no operator found which takes a left-hand operand of type 'std::function<void (const std::chrono::time_point<std::chrono::system_clock,std::chrono::system_clock::duration> &)>' (or there is no acceptable conversion)