Assume there is variadic function template<typename... Args> foo(const S& s, Args... args)
.
struct S {};
template<typename... Args>
void foo(const S& s, Args... args);
void check_foo()
{
S s0{};
S s1{};
char a0 = 0;
short a1 = 1;
int a2 = 2;
foo(s0, a0); // OK
foo(s0, a0, a1); // OK
foo(s1, a1, a2); // OK
}
Is there way to write function bar(const S& s0, Args0...args0, const S& s1, Args1...args1, ..., const S& sN, ArgsN...argsN)
which expands to foo(s0, args0...); foo(s1, args1...); ...; foo(sN, argsN...);
?
I've tried (without success):
#include <type_traits>
template<typename... Args,
typename = std::enable_if_t<
!(... || std::is_same_v<S, Args>)
>
>
void bar(const S& s, Args... args)
{
foo(s, args...);
}
template<typename... Args0, typename... Args1>
void bar(
const S& s0, Args0... args0,
const S& s1, Args1... args1)
{
bar(s0, args0...); // ??? call to foo
bar(s1, args1...); // ??? recursive to bar or to foo
}
void check_bar()
{
S s0{};
S s1{};
char a0 = 0;
short a1 = 1;
int a2 = 2;
bar(s0, a0); // OK
bar(s1, a1, a2); // OK
bar(s0, a0,
s0, a0, a1,
s1, a1, a2); // fail
}
If i understand correctly compiler (clang 9.0.0) always expands Args0 to empty list and matching failed.
Something along these lines:
Demo