Since C++14 cannot use fold expression, in order to make a function that calls bar
on each variadics, one have to use function overloading.
template<typename Arg>
void foo(Arg arg) {
bar(arg);
}
template<typename Arg, typename ...Args>
void foo(Arg arg, Args... args) {
bar(arg);
foo(args...);
}
But using comma operator and parenthesis in bracket, one can unpack without overload.
template<typename ...Args>
void foo(Args... args) {
int dummy[] = {
(bar(args), 0)...
};
}
It works well as expected, but my compiler always warns me that dummy
is not used.
So I'm worried that the compiler removes dummy
(since it's unused and actually initialized only by literals), and resulting to not calling bar
.
Or just declaring dummy
as volatile
is enough to be guarantee that bar
is called?
I know that there is [[maybe_unused]]
attribute, but it's also C++17 feature.
Compiler might remove
dummy
but cannot removebar
calls if they have side effects (as for the recursive version).So you are safe.
That is worst. As you force the write. You remove the warning the wrong way.
Casting to
void
is a common way to remove the warning, previously.