Created
April 21, 2017 23:43
-
-
Save rgriege/f55e997e96fa6677261a8f812d75dc6e to your computer and use it in GitHub Desktop.
C struct initialization in C++
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 <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