Created
February 13, 2018 17:58
-
-
Save garettbass/0f8441179d907090a1ca5cadbf8885d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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