SHARE
TWEET

Variadic Templates - Unforeseen Potential

ascend4nt Jan 3rd, 2013 36 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. // Variadic Templates - Unforeseen potential (see link below)
  4. // Author: Ascend4nt
  5. // https://ascend4nt.wordpress.com/2013/01/03/variadic-template-fun/
  6.  
  7. inline void dummy(...) {}
  8.  
  9. template <typename T>
  10. int repeater(int &n, const T& val)
  11. {
  12.     std::cout << '.';
  13.     ++n;
  14.     // Silence compiler warnings about unused val:
  15.     (void)val;
  16.     return 0;
  17. }
  18.  
  19. template <typename... Args>
  20. void TestVariadic(const Args&... args)
  21. {
  22.     int n = 0;
  23.     dummy(repeater(n, args)...);
  24.  
  25.     std::cout << "\nValue of n:" << n << std::endl;
  26. }
  27.  
  28. int main ()
  29. {
  30.   int p = 5;
  31.   TestVariadic(4, 'a', p, "string");
  32.  
  33.   return 0;
  34. }
RAW Paste Data
Top