Ryan Brown
Ryan Brown

Reputation: 1095

Defining machine-independent datatypes in C++

Is there a way to do this?

#if sizeof(int) == 4
typedef unsigned int Integer32;
#else
typedef unsigned long Integer32;
#endif

or do you have to just #define integer size and compile different headers in?

Upvotes: 0

Views: 679

Answers (1)

Pete Becker
Pete Becker

Reputation: 76523

If you need exact sizes you can use the intXX_t and uintXX_t variants, where XX is 8, 16, 32, or 64.

If you need types that are at least some size, use int_leastXX_t and uint_leastXX_t;

if you need fast, use int_fastXX_t and uint_fastXX_t.

You get these from <stdint.h>, which came in with C99. If you don't have C99 it's a little harder. You can't use sizeof(int) because the preprocessor doesn't know about types. So use INT_MAX (from <limits.h>, etc. to figure out whether a particular type is large enough for what you need.

Upvotes: 2

Related Questions