Vote count:
0
Apologies for the confusing title, I couldn't find a clearer way to express my question. I'm trying to use std::functions for implementing callbacks. The idea is to map "actions" to functions and provide an easy interface to keep track of which functions to call when a certain action happens.
Here's a simplified version of my class:
template<typename ID, typename Return, typename ...Args>
class A
{
public:
void Register(ID id, std::function<Return(Args...)> funct)
{
m_listeners.emplace(id, funct);
}
A() {}
~A() {}
private:
std::unordered_multimap<ID, std::function<Return(Args...)> > m_listeners;
};
What I really want is A to have only 1 template template so when A is declared underlying container can still accept function with any return type and any number of arguments.
I don't have much experience with templates I'm not even sure what I'm trying to do is possible, but I need to hide the template parameters for unordered_multimap from class A.
What I'm trying to do would look something like this:
template<typename ID>
class A
{
public:
template<typename Return, typename ...Args>
void Register(ID id, std::function<Return(Args...)> funct)
{
m_listeners.emplace(id, funct);
}
A() {}
~A() {}
private:
template<typename Return, typename ...Args>
std::unordered_multimap<ID, std::function<Return(Args...)> > m_listeners;
};
Is there any way to achieve this?
Deducing Template Arguments for Member Functions and Variables
Aucun commentaire:
Enregistrer un commentaire