code_junkie

char!=(signed char), char!=(unsigned char)

Nov 13th, 2011
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. cout << getIsTrue< isX<int8>::ikIsX >() << endl;
  2. cout << getIsTrue< isX<uint8>::ikIsX >() << endl;
  3. cout << getIsTrue< isX<char>::ikIsX >() << endl;
  4.  
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. typedef signed char int8;
  10. typedef unsigned char uint8;
  11. typedef signed short int16;
  12. typedef unsigned short uint16;
  13. typedef signed int int32;
  14. typedef unsigned int uint32;
  15. typedef signed long long int64;
  16. typedef unsigned long long uint64;
  17.  
  18. struct TrueType {};
  19. struct FalseType {};
  20.  
  21. template <typename T>
  22. struct isX
  23. {
  24. typedef typename T::ikIsX ikIsX;
  25. };
  26.  
  27.  
  28. // This int==int32 is ambiguous
  29. //template <> struct isX<int > { typedef FalseType ikIsX; }; // Fails
  30. template <> struct isX<int32 > { typedef FalseType ikIsX; };
  31. template <> struct isX<uint32 > { typedef FalseType ikIsX; };
  32.  
  33.  
  34. // Whay isn't this ambiguous? char==int8
  35. template <> struct isX<char > { typedef FalseType ikIsX; };
  36. template <> struct isX<int8 > { typedef FalseType ikIsX; };
  37. template <> struct isX<uint8 > { typedef FalseType ikIsX; };
  38.  
  39.  
  40. template <typename T> bool getIsTrue();
  41. template <> bool getIsTrue<TrueType>() { return true; }
  42. template <> bool getIsTrue<FalseType>() { return false; }
  43.  
  44. int main(int, char **t )
  45. {
  46. cout << sizeof(int8) << endl; // 1
  47. cout << sizeof(uint8) << endl; // 1
  48. cout << sizeof(char) << endl; // 1
  49.  
  50. cout << getIsTrue< isX<int8>::ikIsX >() << endl;
  51. cout << getIsTrue< isX<uint8>::ikIsX >() << endl;
  52. cout << getIsTrue< isX<char>::ikIsX >() << endl;
  53.  
  54. cout << getIsTrue< isX<int32>::ikIsX >() << endl;
  55. cout << getIsTrue< isX<uint32>::ikIsX >() << endl;
  56. cout << getIsTrue< isX<int>::ikIsX >() << endl;
  57.  
  58. }
Add Comment
Please, Sign In to add comment