Background
I'm trying to write some template functions for a template-only unit test library, specifically for Qt.
Problem
In this library, I have a variadic template that receives a variable amount of objects and functors (Qt5 Signals actually), always paired next to each other, as in QObject, signal, etc... then desirably followed by a variable amount of signal arguments.
Desired Solution
// implementation.h
template <typename T, typename U, typename... Sargs, typename... Fargs>
void test_signal_daisy_chain(T* t, void(T::*t_signal)(Fargs...), U* u, void(U::*u_signal)(Fargs...), Sargs... sargs, Fargs... fargs){...}
// client.cpp
test_signal_daisy_chain(object, &Object::signal1, object, &Object::signal2, object, &Object::signal3, 1, 2, 3); // where the signals are defined as void(Object::*)(int, int, int)
Unsurprisingly I get "no matching function" due to "template argument deduction/substitution failed", and my ClangCodeModel plugin warns me that 6 arguments were expected, where 8 were given.
Working (ugly) solution
// implementation.h
template <typename... Fargs>
struct wrapper
{
template <typename T, typename U, typename... Sargs>
void test_signal_daisy_chain(Fargs... fargs, T* t, void(T::*t_signal)(Fargs...), U* u, void(U::*u_signal)(Fargs...), Sargs... sargs){...}
// client.cpp
wrapper<int, int, int>::test_signal_daisy_chain(1, 2, 3, object, &Object::signal1, object, &Object::signal2, object, &Object::signal3);
I'm not content with having to explicitly define the variable function arguments at both the beginning of the function call and in the wrapper template type parameters. In fact, I was initially surprised that the could not be deduced simply by the fact that they were to match the variable arguments of the functors. I'm open to using wrapper functions as opposed to wrapper classes, as I already have a detail namespace set up which I'm willing to get messy for in order to provide a clean and user-friendly API.
Note: signal arguments can be anywhere from primitives to user-defined types to POD structs to template classes, all of variable length.
Aucun commentaire:
Enregistrer un commentaire