Guest User

Untitled

a guest
Feb 25th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct Foo
  4. {
  5. int a;
  6. int b;
  7. };
  8.  
  9. template<typename T, typename Struct>
  10. constexpr size_t offset_of(T Struct::*member) {
  11. using pointer = Struct*;
  12. return size_t(&(pointer(nullptr)->*member));
  13. }
  14.  
  15. enum : size_t {
  16. offset_of_Foo_a = offset_of(&Foo::a),
  17. offset_of_Foo_b = offset_of(&Foo::b),
  18. };
  19.  
  20. int main() {
  21. std::cout<< "offset_of_Foo_a: " << offset_of_Foo_a << "\n";
  22. std::cout<< "offset_of_Foo_b: " << offset_of_Foo_b << "\n";
  23. return 0;
  24. }
Add Comment
Please, Sign In to add comment