fork download
  1. #include <iostream>
  2.  
  3. typedef unsigned char byte;
  4. typedef char int8;
  5. typedef unsigned char uint8;
  6. typedef short int16;
  7. typedef unsigned short uint16;
  8. typedef int int32;
  9. typedef unsigned int uint32;
  10. typedef long long int64;
  11. typedef unsigned long long uint64;
  12. typedef float single;
  13.  
  14. template < byte b0, byte b1, byte b2, byte b3, byte b4 = b0 + 4, byte b5 = b1 + 4, byte b6 = b2 + 4, byte b7 = b3 + 4 >
  15. struct ByteOrder {
  16. const static byte Positions[8];
  17.  
  18. static int8 Int8 ( int8 value ) {
  19. return Value( value );
  20. }
  21.  
  22. static int16 Int16 ( int16 value ) {
  23. return Value( value );
  24. }
  25.  
  26. static int32 Int32 ( int32 value ) {
  27. return Value( value );
  28. }
  29.  
  30. static int64 Int64 ( int64 value ) {
  31. return Value( value );
  32. }
  33.  
  34. static uint8 UInt8 ( uint8 value ) {
  35. return Value( value );
  36. }
  37.  
  38. static uint16 UInt16 ( uint16 value ) {
  39. return Value( value );
  40. }
  41.  
  42. static uint32 UInt32 ( uint32 value ) {
  43. return Value( value );
  44. }
  45.  
  46. static uint64 UInt64 ( uint64 value ) {
  47. return Value( value );
  48. }
  49.  
  50. static single Single ( single value ) {
  51. return Value( value );
  52. }
  53.  
  54. static double Double ( double value ) {
  55. return Value( value );
  56. }
  57.  
  58. template <typename T>
  59. static T Value ( T value ) {
  60. T out = 0;
  61. byte* b = (byte*)&value;
  62. byte* bout = (byte*)&out;
  63. for ( size_t p = 0; p < sizeof( value ); ++p ) {
  64. bout[p] = b[ Positions[p] ];
  65. }
  66. return out;
  67. }
  68.  
  69. };
  70.  
  71. template < byte b0, byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7>
  72. const byte BaseByteOrder<b0, b1, b2, b3, b4, b5, b6, b7>::Positions[8] = { b0, b1, b2, b3, b4, b5, b6, b7 };
  73.  
  74. template <int16 order>
  75. struct ByteOrder<order, order, order, order, order, order, order, order> {
  76. const static byte Positions[8];
  77.  
  78. static int8 Int8 ( int8 value ) {
  79. return Value( value );
  80. }
  81.  
  82. static int16 Int16 ( int16 value ) {
  83. return Value( value );
  84. }
  85.  
  86. static int32 Int32 ( int32 value ) {
  87. return Value( value );
  88. }
  89.  
  90. static int64 Int64 ( int64 value ) {
  91. return Value( value );
  92. }
  93.  
  94. static uint8 UInt8 ( uint8 value ) {
  95. return Value( value );
  96. }
  97.  
  98. static uint16 UInt16 ( uint16 value ) {
  99. return Value( value );
  100. }
  101.  
  102. static uint32 UInt32 ( uint32 value ) {
  103. return Value( value );
  104. }
  105.  
  106. static uint64 UInt64 ( uint64 value ) {
  107. return Value( value );
  108. }
  109.  
  110. static single Single ( single value ) {
  111. return Value( value );
  112. }
  113.  
  114. static double Double ( double value ) {
  115. return Value( value );
  116. }
  117.  
  118. template <typename T>
  119. static T Value ( T value ) {
  120. T out = 0;
  121. byte* b = (byte*)&value;
  122. byte* bout = (byte*)&out;
  123. for ( size_t p = 0; p < sizeof( value ); ++p ) {
  124. bout[p] = b[ Positions[p] ];
  125. }
  126. return out;
  127. }
  128. };
  129.  
  130. int main (int argc, char* argv[]) {
  131.  
  132. std::cout << std::hex;
  133. std::cout << "Argc before: " << argc << std::endl;
  134. std::cout << "Argc with 0x3210: " << ByteOrder<0x3210>::UInt64( argc ) << std::endl;
  135. //std::cout << "Argc with 0x32107654: " << ByteOrder<(int)0x32107654>::UInt64( argc ) << std::endl;
  136. //std::cout << "Argc with 0x76543210: " << ByteOrder<(int)0x76543210>::UInt64( argc ) << std::endl;
  137.  
  138. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Hm. Short or long?
compilation info
prog.cpp:72:25: error: expected initializer before '<' token
prog.cpp: In function 'int main(int, char**)':
prog.cpp:134:58: error: wrong number of template arguments (1, should be 8)
prog.cpp:15:8: error: provided for 'template<unsigned char b0, unsigned char b1, unsigned char b2, unsigned char b3, unsigned char b4, unsigned char b5, unsigned char b6, unsigned char b7> struct ByteOrder'
stdout
Standard output is empty