Skip to content

Instantly share code, notes, and snippets.

@sw17ch

sw17ch/newtype.c

Created Oct 13, 2009
Embed
What would you like to do?
#include <stdio.h>
// Make a NEWTYPE
#define NT(inner,outer) \
typedef struct \
{ \
inner _nt_field; \
} outer
// Declare a val of NEWTYPE
#define DECLNT(outer,name,init) outer name = { init }
// Inspect the value of a NEWTYPE
#define VALNT(x) (x)._nt_field
NT(short,INT16);
NT(int, INT32);
INT16 increment_16(INT16 v)
{
VALNT(v)++;
return v;
}
INT32 increment_32(INT32 v)
{
VALNT(v)++;
return v;
}
int main(int argc, char * argv[])
{
DECLNT(INT16, i_16, 15);
DECLNT(INT32, i_32, 15);
printf("%d\n", VALNT(i_16));
printf("%d\n", VALNT(i_32));
i_16 = increment_16(i_16);
i_32 = increment_32(i_32);
printf("%d\n", VALNT(i_16));
printf("%d\n", VALNT(i_32));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.