fork download
  1. #include <cstdlib>
  2. #include <cassert>
  3.  
  4. #pragma pack (1)
  5.  
  6. template <size_t Width>
  7. class Base
  8. {
  9. public:
  10. char mData [Width];
  11. template <typename Field> Field ExtractAs () const
  12. {
  13. return *reinterpret_cast <Field> (mData);
  14. }
  15. };
  16.  
  17. template <typename FieldVal>
  18. class IntegralField
  19. :
  20. public Base <sizeof (FieldVal)>
  21. {
  22. public:
  23. FieldVal GetVal () const
  24. {
  25. return ExtractAs <FieldVal> ();
  26. }
  27. };
  28.  
  29. int main()
  30. {
  31. char raw[4] = {0x11, 0x22, 0x33, 0x44};
  32. typedef IntegralField <uint32_t> UInt32Field;
  33. const UInt32Field& field =
  34. *reinterpret_cast <const UInt32Field*> (raw);
  35. const uint32_t extracted = field.GetVal();
  36. assert (extracted == 0x11223344);
  37. }
  38.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In member function ‘FieldVal IntegralField<FieldVal>::GetVal() const’:
prog.cpp:25:16: error: ‘ExtractAs’ was not declared in this scope
prog.cpp:25:35: error: expected primary-expression before ‘>’ token
prog.cpp:25:38: error: expected primary-expression before ‘)’ token
prog.cpp: In function ‘int main()’:
prog.cpp:32:28: error: ‘uint32_t’ was not declared in this scope
prog.cpp:32:36: error: template argument 1 is invalid
prog.cpp:32:49: error: invalid type in declaration before ‘;’ token
prog.cpp:34:52: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
prog.cpp:35:11: error: ‘uint32_t’ does not name a type
prog.cpp:36:5: error: ‘extracted’ was not declared in this scope
prog.cpp:33:24: warning: unused variable ‘field’ [-Wunused-variable]
stdout
Standard output is empty