fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. template <typename StrType, typename EnumType>
  6. class ExampleClass
  7. {};
  8.  
  9. template <typename StrType, typename EnumType>
  10. void fooSingleCase()
  11. {
  12. std::cout << "In: " << __PRETTY_FUNCTION__ << "\n";
  13. ExampleClass<StrType, EnumType> instance;
  14. // ...
  15. }
  16.  
  17. template <typename StrType>
  18. struct StrTraits
  19. {
  20. static const std::string pattern;
  21. };
  22.  
  23. enum class ENUM1
  24. {
  25. VAL1,
  26. VAL2,
  27. VAL3,
  28. VAL4
  29. };
  30.  
  31. template <typename EnumType>
  32. struct ENUM1Traits
  33. {
  34. static const ENUM1 pattern;
  35. };
  36.  
  37. class VAL1TYPE {};
  38. class VAL2TYPE {};
  39. class VAL3TYPE {};
  40.  
  41. class STR1TYPE {};
  42. class STR2TYPE {};
  43.  
  44. template <>
  45. const std::string StrTraits<STR1TYPE>::pattern = "str1";
  46. template <>
  47. const std::string StrTraits<STR2TYPE>::pattern = "str2";
  48.  
  49. template <>
  50. const ENUM1 ENUM1Traits<VAL1TYPE>::pattern = ENUM1::VAL1;
  51. template <>
  52. const ENUM1 ENUM1Traits<VAL2TYPE>::pattern = ENUM1::VAL2;
  53. template <>
  54. const ENUM1 ENUM1Traits<VAL3TYPE>::pattern = ENUM1::VAL3;
  55.  
  56. template <typename EnumType>
  57. void fooFixedEnumCase(std::string strVal)
  58. {
  59. std::cout << strVal << " not found!\n";
  60. }
  61.  
  62. template <typename EnumType, typename Str1Type, typename ...StrType>
  63. void fooFixedEnumCase(std::string strVal)
  64. {
  65. if (StrTraits<Str1Type>::pattern == strVal)
  66. fooSingleCase<EnumType, Str1Type>();
  67. else
  68. fooFixedEnumCase<EnumType, StrType...>(strVal);
  69. }
  70.  
  71. template <typename ...EnumType>
  72. struct Enum1Switch;
  73.  
  74. template <typename EnumType1, typename ...EnumType>
  75. struct Enum1Switch<EnumType1, EnumType...>
  76. {
  77. template <typename ...StrType>
  78. static void strSwitch(std::string str, ENUM1 enumVal)
  79. {
  80. if (ENUM1Traits<EnumType1>::pattern == enumVal)
  81. fooFixedEnumCase<EnumType1, StrType...>(str);
  82. else
  83. Enum1Switch<EnumType...>::template strSwitch<StrType...>(str, enumVal);
  84. }
  85. };
  86.  
  87. template <>
  88. struct Enum1Switch<>
  89. {
  90. template <typename ...StrType>
  91. static void strSwitch(std::string str, ENUM1 enumVal)
  92. {
  93. std::cout << static_cast<int>(enumVal) << " not found!\n";
  94. }
  95. };
  96.  
  97. void foo(std::string str, ENUM1 enumVal) {
  98.  
  99. Enum1Switch<VAL1TYPE, VAL2TYPE, VAL3TYPE>
  100. ::strSwitch<STR1TYPE, STR2TYPE>(str, enumVal);
  101. }
  102.  
  103. int main() {
  104. foo("str2", ENUM1::VAL3);
  105.  
  106. foo("str1", ENUM1::VAL1);
  107.  
  108. foo("str4", ENUM1::VAL1);
  109.  
  110. foo("str2", ENUM1::VAL4);
  111. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
In: void fooSingleCase() [with StrType = VAL3TYPE; EnumType = STR2TYPE]
In: void fooSingleCase() [with StrType = VAL1TYPE; EnumType = STR1TYPE]
str4 not found!
3 not found!