Skip to content

Instantly share code, notes, and snippets.

@garettbass
Created February 13, 2018 17:58
Show Gist options
  • Save garettbass/0f8441179d907090a1ca5cadbf8885d1 to your computer and use it in GitHub Desktop.
Save garettbass/0f8441179d907090a1ca5cadbf8885d1 to your computer and use it in GitHub Desktop.
#include <iostream>
struct Foo
{
int a;
int b;
};
template<typename T, typename Struct>
constexpr size_t offset_of(T Struct::*member) {
using pointer = Struct*;
return size_t(&(pointer(nullptr)->*member));
}
enum : size_t {
offset_of_Foo_a = offset_of(&Foo::a),
offset_of_Foo_b = offset_of(&Foo::b),
};
int main() {
std::cout<< "offset_of_Foo_a: " << offset_of_Foo_a << "\n";
std::cout<< "offset_of_Foo_b: " << offset_of_Foo_b << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment