Skip to content

Instantly share code, notes, and snippets.

@a-square
Created April 9, 2019 12:55
Show Gist options
  • Save a-square/d50086dff136e9c2a8c1fdaa68c05ce7 to your computer and use it in GitHub Desktop.
Save a-square/d50086dff136e9c2a8c1fdaa68c05ce7 to your computer and use it in GitHub Desktop.
Binary value of (int TFoo::*)nullptr
#include <cstdint>
#include <cstring>
#include <iostream>
struct TFoo {
int Bar;
int Baz;
};
template <typename T, typename U>
inline T bit_cast(const U& value) {
static_assert(sizeof(T) == sizeof(U));
T result;
memcpy(&result, &value, sizeof(value));
return result;
}
int main() {
std::cout
<< "Equality: "
<< (&TFoo::Bar == nullptr) << ' '
<< (&TFoo::Baz == nullptr) << std::endl;
std::cout
<< "Representation: "
<< "ULONG_MAX = " << ULONG_MAX << ", "
<< "(int TFoo::*)nullptr = " << bit_cast<size_t>((int TFoo::*)nullptr) << ", "
<< "&TFoo::Bar = " << bit_cast<size_t>(&TFoo::Bar) << ", "
<< "&TFoo::Baz = " << bit_cast<size_t>(&TFoo::Baz) << std::endl;
std::cout
<< "Sizes: "
<< sizeof(&TFoo::Bar) << ' '
<< sizeof(&TFoo::Baz) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment