samedi 20 décembre 2014

Variadic-templated class: support void without specializing the whole class?

I have a class:



template <typename ...Arguments>
class CSignalConnection
{
public:
CSignalConnection(std::function<void(Arguments...)> target) : m_target(target) {}

void invoke(Arguments&&... args) const
{
m_target(std::forward<Arguments>(args)...);
}

private:
std::function<void(Arguments...)> m_target;
};


I want to be able to declare CSignalConnection<void> and call invoke with no arguments. And I want to avoid specializing, i. e. duplicating the whole class, when I only have a couple void-incompatible methods (like invoke here which should be declared with 0 arguments). I have a couple more classes like this so I hate to write everything twice (and edit in two places).


One idea I have is to write both void and non-void invoke overloads and disable one with SFINAE, but I don't know how to actually implement this.


Aucun commentaire:

Enregistrer un commentaire