Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. #include <cstdint>
  2. #include <cstring>
  3. #include <iostream>
  4.  
  5.  
  6. struct TFoo {
  7. int Bar;
  8. int Baz;
  9. };
  10.  
  11.  
  12. template <typename T, typename U>
  13. inline T bit_cast(const U& value) {
  14. static_assert(sizeof(T) == sizeof(U));
  15.  
  16. T result;
  17. memcpy(&result, &value, sizeof(value));
  18. return result;
  19. }
  20.  
  21.  
  22. int main() {
  23. std::cout
  24. << "Equality: "
  25. << (&TFoo::Bar == nullptr) << ' '
  26. << (&TFoo::Baz == nullptr) << std::endl;
  27.  
  28. std::cout
  29. << "Representation: "
  30. << "ULONG_MAX = " << ULONG_MAX << ", "
  31. << "(int TFoo::*)nullptr = " << bit_cast<size_t>((int TFoo::*)nullptr) << ", "
  32. << "&TFoo::Bar = " << bit_cast<size_t>(&TFoo::Bar) << ", "
  33. << "&TFoo::Baz = " << bit_cast<size_t>(&TFoo::Baz) << std::endl;
  34.  
  35. std::cout
  36. << "Sizes: "
  37. << sizeof(&TFoo::Bar) << ' '
  38. << sizeof(&TFoo::Baz) << std::endl;
  39.  
  40. return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement