fork download
  1. #include <type_traits>
  2. #include <iostream>
  3.  
  4. //
  5. // My own addition to type traits
  6. //
  7.  
  8. // Just a set of types
  9. template <class... Args>
  10. struct types {};
  11.  
  12. // true_type if T matches any types in Args, false_type otherwise.
  13. template <class T, class... Args>
  14. struct is_any;
  15.  
  16. template <class T, class Head, class... Tail>
  17. struct is_any<T, Head, Tail...> : std::integral_constant<bool, is_any<T, Tail...>::value>
  18. {};
  19.  
  20. template <class T, class... Tail>
  21. struct is_any<T, T, Tail...> : std::true_type
  22. {};
  23.  
  24. template <class T>
  25. struct is_any<T> : std::false_type
  26. {};
  27.  
  28. template <class T, class... Args1, class... Args2>
  29. struct is_any<T, types<Args1...>, Args2...> : std::integral_constant<bool, is_any<T, Args1...>::value || is_any<T, Args2...>::value>
  30. {};
  31.  
  32. // Here's a shortcut, because having this syntax in every function I write is painful.
  33. template <class T, class... Args>
  34. using enable_types = typename std::enable_if<is_any<T, Args...>::value>::type*;
  35.  
  36. //
  37. // Testing
  38. //
  39.  
  40. struct red {};
  41. struct green {};
  42. struct blue {};
  43. struct cereal {};
  44. struct icecream {};
  45. struct pizza {};
  46. struct tire {};
  47. struct engine {};
  48. struct transmission {};
  49.  
  50. using color_category = types<red, green, blue>;
  51. using food_category = types<cereal, icecream, pizza>;
  52. using part_category = types<tire, engine, transmission>;
  53.  
  54. template <typename T>
  55. void categorize(enable_types<T, color_category> = 0)
  56. {
  57. std::cout << "T is a color" << std::endl;
  58. }
  59.  
  60. template <typename T>
  61. void categorize(enable_types<T, food_category, part_category> = 0)
  62. {
  63. std::cout << "T is a food or a part" << std::endl;
  64. }
  65.  
  66. template <typename T>
  67. void categorize(enable_types<T, float, double, int> = 0)
  68. {
  69. std::cout << "T is a float, double, or an int" << std::endl;
  70. }
  71.  
  72. int main()
  73. {
  74. categorize<int>();
  75. categorize<red>();
  76. categorize<icecream>();
  77. categorize<tire>();
  78. return 0;
  79. }
  80.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:34:1: error: expected unqualified-id before 'using'
prog.cpp:50:7: error: expected nested-name-specifier before 'color_category'
prog.cpp:50:7: error: 'color_category' has not been declared
prog.cpp:50:22: error: expected ';' before '=' token
prog.cpp:50:22: error: expected unqualified-id before '=' token
prog.cpp:51:7: error: expected nested-name-specifier before 'food_category'
prog.cpp:51:7: error: 'food_category' has not been declared
prog.cpp:51:21: error: expected ';' before '=' token
prog.cpp:51:21: error: expected unqualified-id before '=' token
prog.cpp:52:7: error: expected nested-name-specifier before 'part_category'
prog.cpp:52:7: error: 'part_category' has not been declared
prog.cpp:52:21: error: expected ';' before '=' token
prog.cpp:52:21: error: expected unqualified-id before '=' token
prog.cpp:55:17: error: variable or field 'categorize' declared void
prog.cpp:55:17: error: 'enable_types' was not declared in this scope
prog.cpp:55:31: error: expected primary-expression before ',' token
prog.cpp:55:33: error: 'color_category' was not declared in this scope
prog.cpp:55:49: error: expected primary-expression before '=' token
prog.cpp:61:17: error: variable or field 'categorize' declared void
prog.cpp:61:17: error: 'enable_types' was not declared in this scope
prog.cpp:61:31: error: expected primary-expression before ',' token
prog.cpp:61:33: error: 'food_category' was not declared in this scope
prog.cpp:61:48: error: 'part_category' was not declared in this scope
prog.cpp:61:63: error: expected primary-expression before '=' token
prog.cpp:67:17: error: variable or field 'categorize' declared void
prog.cpp:67:17: error: 'enable_types' was not declared in this scope
prog.cpp:67:31: error: expected primary-expression before ',' token
prog.cpp:67:33: error: expected primary-expression before 'float'
prog.cpp:67:40: error: expected primary-expression before 'double'
prog.cpp:67:48: error: expected primary-expression before 'int'
prog.cpp: In function 'int main()':
prog.cpp:74:5: error: 'categorize' was not declared in this scope
prog.cpp:74:16: error: expected primary-expression before 'int'
prog.cpp:74:16: error: expected ';' before 'int'
prog.cpp:75:19: error: expected primary-expression before '>' token
prog.cpp:75:21: error: expected primary-expression before ')' token
prog.cpp:76:24: error: expected primary-expression before '>' token
prog.cpp:76:26: error: expected primary-expression before ')' token
prog.cpp:77:20: error: expected primary-expression before '>' token
prog.cpp:77:22: error: expected primary-expression before ')' token
stdout
Standard output is empty