Skip to content

Instantly share code, notes, and snippets.

@rgriege
Created April 21, 2017 23:43
Show Gist options
  • Save rgriege/f55e997e96fa6677261a8f812d75dc6e to your computer and use it in GitHub Desktop.
Save rgriege/f55e997e96fa6677261a8f812d75dc6e to your computer and use it in GitHub Desktop.
C struct initialization in C++
#include <cstdio>
#include <cstdlib>
#define INIT_OPEN(type) [] { type _;
#define INIT_CLOSE ; return _; }()
#define INIT(type, body) INIT_OPEN(type) body INIT_CLOSE
struct A {
int i;
};
#define A(body) INIT(A, body)
static const A g_a = A( _.i = 420 );
int main() {
A a = A( _.i = rand() );
printf("%d\n", a.i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment