Advertisement
ascend4nt

Variadic Templates - Transformations

Jan 3rd, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. // Variadic Templates - Transform Example (see link below for description)
  4. // Author: Ascend4nt
  5. // https://ascend4nt.wordpress.com/2013/01/03/variadic-templates-transformations/
  6.  
  7. inline void dummy(...) {}
  8.  
  9. int reportBoolState(bool bVal)
  10. {
  11.     std::cout << bVal << "  ";
  12.     return 0;
  13. }
  14.  
  15. template <typename... Args>
  16. int reportBoolState(bool bVal, const Args&... args)
  17. {
  18.     std::cout << bVal << "  ";
  19.     reportBoolState(args...);
  20.     return 0;
  21. }
  22.  
  23. template <typename... Args>
  24. void bool_variadic(const Args&... args)
  25. {
  26.     std::cout << "\nbool states (in FORWARD order):\n" << std::boolalpha;
  27.     //dummy(reportBoolState(args)...);  // this'd be reverse order (right-to-left)
  28.     reportBoolState(args...);
  29.     std::cout << std::endl;
  30. }
  31.  
  32. template <typename T>
  33. bool boolizer(const T& val)
  34. {
  35.     std::cout <<  val << "  ";
  36.     return (val != 0);
  37. }
  38.  
  39. template <typename... Args>
  40. void TestVariadic(const Args&... args)
  41. {
  42.     std::cout << "vals (in reverse):" << std::endl;
  43.     bool_variadic(boolizer(args)...);
  44. }
  45.  
  46. int main ()
  47. {
  48.   int p = 5;
  49.   TestVariadic(0, 4, 'a', '\0', p, 0, "string");
  50.  
  51.   return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement