fork download
  1. #include <iostream>
  2. #include <utility>
  3.  
  4. struct Foo
  5. {
  6. template <typename T>
  7. void setArg(int i, const T& t)
  8. {
  9. std::cout << "setArg(" << i << ", " << t << ")\n";
  10. }
  11. };
  12.  
  13. namespace detail
  14. {
  15. template <std::size_t ... Is, typename ... Ts>
  16. void SetFoo(Foo& foo, std::index_sequence<Is...>, Ts&&... args)
  17. {
  18. int dummy[] = {0, (foo.setArg(Is, args), void(), 0)...};
  19. (void) dummy; // Remove warning for unused variable
  20. }
  21. }
  22.  
  23. template <typename ... Ts>
  24. void SetFoo(Foo& foo, Ts&&... args)
  25. {
  26. detail::SetFoo(foo, std::index_sequence_for<Ts...>(), std::forward<Ts>(args)...);
  27. }
  28.  
  29.  
  30. int main() {
  31. Foo foo;
  32.  
  33. SetFoo(foo, '*', 42, "hello world", 4.2f);
  34. }
  35.  
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
setArg(0, *)
setArg(1, 42)
setArg(2, hello world)
setArg(3, 4.2)