Is unpacking variadic using array(or initializer_list) trick optimize-safe?

  argument-unpacking, c++, c++14, optimization, variadic

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.

Source: Windows Questions C++

LEAVE A COMMENT