fork download
  1. #include <iostream>
  2. #include <typeinfo>
  3.  
  4. using namespace std;
  5.  
  6. template<typename FalseType, typename TrueType, bool condition>
  7. struct ConditionalTypeSelector
  8. {
  9. typedef void ResultType;
  10. };
  11.  
  12. template<typename FalseType, typename TrueType>
  13. struct ConditionalTypeSelector<FalseType,TrueType,false>
  14. {
  15. typedef FalseType ResultType;
  16. };
  17.  
  18. template<typename FalseType, typename TrueType>
  19. struct ConditionalTypeSelector<FalseType,TrueType,true>
  20. {
  21. typedef TrueType ResultType;
  22. };
  23.  
  24. struct A {
  25. unsigned char member;
  26. };
  27.  
  28. struct B {
  29. int member;
  30. };
  31.  
  32. struct C {
  33. long long member;
  34. };
  35.  
  36. int main() {
  37. cout << typeid
  38. ( ConditionalTypeSelector
  39. < A,B,(sizeof(A) > sizeof(B))
  40. >::ResultType
  41. ).name() << endl;
  42. cout << typeid
  43. ( ConditionalTypeSelector
  44. < A,B,(sizeof(B) > sizeof(A))
  45. >::ResultType
  46. ).name() << endl;
  47.  
  48. cout << typeid
  49. ( ConditionalTypeSelector
  50. < A,C,(sizeof(A) > sizeof(C))
  51. >::ResultType
  52. ).name() << endl;
  53.  
  54. cout << typeid
  55. ( ConditionalTypeSelector
  56. < C,B,true>::ResultType
  57. ).name() << endl;
  58.  
  59. cout << typeid
  60. ( ConditionalTypeSelector
  61. < C,A,false>::ResultType
  62. ).name() << endl;
  63.  
  64. return 0;
  65. }
  66.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
1A
1B
1A
1B
1C