lundi 14 août 2017

variadic template with function(args)... expansion

Today, my colleague showed me an amazing c++ expansion trick by varadic template with function calls. In succinct, it looks like:

template <typename T, T... I>
void print_sequence(std::integer_sequence<T, I...>) {
    std::initializer_list<bool>{ bool(std::cout << I << ' ')... };
}

Then I find something similar from wiki/Variadic_template, and stackoverflow Unpacking a typelist e.g,

  template<typename... Args> inline void pass(Args&&...) {}
  template<typename... Args> inline void expand(Args&&... args) {
    pass( some_function(args)... );
  }
  expand(42, "answer", true);

i really struggle with this style some_function(args)..., even by reading the above wiki link. How does this work? and why it has to be non-void return type? does it solely work with function or any kind of applicable expression? e.g., from another stackoverflow post Converting Variadic template pack into std::initializer_list

template<typename ...Args>
void foo () {
  fun({Args::value...});
} 

is the above ... the same context as the one with function calls?

Aucun commentaire:

Enregistrer un commentaire