Skip to content

Instantly share code, notes, and snippets.

@dannas
Created May 11, 2020 19:30
Show Gist options
  • Save dannas/976c254e6a645a438ad0ab0d98d76fa7 to your computer and use it in GitHub Desktop.
Save dannas/976c254e6a645a438ad0ab0d98d76fa7 to your computer and use it in GitHub Desktop.
Experiment with union/struct field access generation
// Experiment with expressions for reading from structs and union fields.
// https://godbolt.org/z/G_fsbD
typedef union {
struct {
long u;
short v;
char w;
} t1;
struct {
int a[2];
char *p;
} t2;
} u_type;
#define CONCAT_IMPL(x, y) x##y
#define CONCAT(x, y) CONCAT_IMPL(x, y)
#define GET(Dest, expr) void CONCAT(get, __COUNTER__)(u_type *up, Dest *dest) { *dest = expr;}
GET(long, up->t1.u)
GET(short, up->t1.v)
GET(char*, &up->t1.w)
GET(int*, up->t2.a)
GET(int, up->t2.a[up->t1.u])
GET(char, *up->t2.p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment