Typedef'ed type must be unsigned

  • Thread starter Tomás Ó hÉilidhe
  • Start date
T

Tomás Ó hÉilidhe

I'm writing some cross-platform code and I invite the developer to
pick their own integer types in places. However, the integer type must be
unsigned. So far, I make sure this is so with:

typedef int SegIMustBeUnsigned[(SegI)-1 > 0 ? 3 : -3];

Yes, this does the trick, but I'm just wondering if anyone here has a
better way of doing it.
 
P

Peter Nilsson

Tomás Ó hÉilidhe said:
I'm writing some cross-platform code and I invite the
developer to pick their own integer types in places.
However, the integer type must be unsigned.

BTW, be careful about allowing unsigned types that promote
to (signed) int.
So far, I make sure this is so with:

typedef int SegIMustBeUnsigned[(SegI)-1 > 0 ? 3 : -3];

Yes, this does the trick, but I'm just wondering if
anyone here has a better way of doing it.

Google for compile time asserts. There are a few ways, but
none of them are any more or less elegant that what you
have.
 

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

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,160
Latest member
CollinStri
Top