Created
January 10, 2018 10:43
-
-
Save Liam0205/78725c33de5545335ee81235cd9f5f0e to your computer and use it in GitHub Desktop.
A customized `offsetof` implement, in modern C++.
This file contains hidden or 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 <stdio.h> | |
#define offsetof(s, m) reinterpret_cast<size_t>(&reinterpret_cast<const volatile char&>(\ | |
static_cast<s*>(nullptr)->m)) | |
struct S { | |
char c; | |
double d; | |
char cc; | |
}; | |
int main(void) { | |
printf("the first element is at offset %zu\n", offsetof(struct S, c)); | |
printf("the double is at offset %zu\n", offsetof(struct S, d)); | |
printf("the third element is at offset %zu\n", offsetof(struct S, cc)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment