c++variadic-templatesc++17stdtupleindex-sequence

Call member function for each element of std::tuple


I've been searching the most simple and elegant solution and found this:

#include <iostream>
#include <utility>
#include <tuple>


struct A{
  void hi(){
    std::cout << "HI\n";
  }
};

template<typename Tuple, std::size_t... Is>
void unpack(Tuple& tpl, std::index_sequence<Is...>){
  ((std::get<Is>(tpl).hi()),...);
}

int main(){

  std::tuple<A,A,A,A,A,A> tpl { A(), A(), A(), A(), A(), A() };

  unpack(tpl, std::make_index_sequence<6>{});

  return 0;
}

However, i do not completely understand this line:

((std::get<Is>(tpl).hi()),...);

I understand what it does, but what is that kind of syntax?

Is it somewhere in standard?


Solution

  • This is a fold expression that does

    (std::get<Is>(tpl).hi())
    

    for every Is that is packed inside.