how to define an 8 bit integer

H

Hallvard B Furuseth

James said:
Chris said:
typedef char test[
sizeof(char * 4) == 32 / CHAR_BIT
];

I understand what you're trying to do there, but wouldn't a #if/#endif
pair bracking a #error directive do what you're trying to do in a much
clearer way?

Yes, #if/#error/#endif is better when you can use preprocessor constants
for your check. Like in this case, since sizeof(char) can be dropped.

If you want to check e.g. sizeof or enum values at compile time, you
need to force a constraint violation after preprocessing. Or two
violations, since some compilers are sloppy about checking them. I
prefer to macroize that to make the actual assertion readable:

#define static_assert(name, c) \
typedef struct { int Assert_##name: 2-4*(c); } Assert_##name[2-4*(c)]

/* TODO: Port this to saner assumptions */
static_assert(supported_atomic, sizeof(sig_atomic_t) * CHAR_BIT >= 16);
 
H

Hallvard B Furuseth

I said:
#define static_assert(name, c) \
typedef struct { int Assert_##name: 2-4*(c); } Assert_##name[2-4*(c)]

Of course, it would have helped if I didn't invert the test by trying to
make the typedef a one-liner.

BTW, the 2 and 4 is just because I have no idea if compilers can be
relied on to handle 'int foo:1;' well when int bitfields are signed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top