I have a variadic template function that I want to use to conditionally add arguments to another variadic template function. This is a minimal example, but I can't get it to compile:
// Copyright 2019 Google LLC.
// SPDX-License-Identifier: Apache-2.0
#include <utility>
template<typename... Args>
void f(Args&&... args) {
// does something with args
}
template<typename T, typename... Args>
void g(T&& t, int i, Args&&... args) {
if (i != 0) t(i, std::forward<Args>(args)...);
else t(std::forward<Args>(args)...);
}
int main() {
g(f, 0, 0);
return 0;
}
The output from clang for the code above is:
main.cpp:15:7: error: no matching function for call
to 'g'
g(f, 0, 0);
^
main.cpp:9:10: note: candidate template ignored:
couldn't infer template argument 'T'
void g(T&& t, int i, Args&&... args) {
^
1 error generated.
With macros, it would work like this (compiles if I replace the g function above with this macro):
// Copyright 2019 Google LLC.
// SPDX-License-Identifier: Apache-2.0
#define g(t, i, args...) \
if((i) != 0) (t)((i), args); \
else (t)(args);
Is there a way I can make this work without macros?