Type Support

Sage
Posts: 1,066
Joined: 2004.07
Post: #1
In consideration of my project being cross platform, I know there is the issue of endianness to consider, but what about support for types? Is there a way to check if the compiler supports the type? Something like #if defined(bool) or anything? Thanks for the help. Smile
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #2
If you just stick to the types that are included with the language, you should be safe. As in you can use bool for C++, but use int for C. Additionally, look in types.h. It will list various types for specific sizes, and is part of the C standard library so you should be fairly sure it will always be there.
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #3
Hm. Ok. I thought bool varied by compiler. That works then. My project is C++ so I'm all set.
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #4
"bool" is supported by all (modern) C++ compilers. "bool" is also part of the C99 standard for regular C, iirc.

The issue you have to be aware of, is that the different types might mean different things. The only requirement of the C/C++ standard is that long >= int >= short >= char, and bool can be anything. So, depending on the target system, the sizes of these types may vary.
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #5
AFAIK 4 bytes is the convention for bool. (not a standard, though) GCC has an option to use 1 byte, but you have to specify that yourself. If you really need to know the size, you can use the size-specific types in types.h. (you could just use those types to hold the info while you're doing whatever to it, and the normal types when it's just hanging out in memory for convenience sake, if you wanted; For just about everything but file IO and networking, it doesn't really matter, same as the byte order, as long as you have enough bytes to hold your information)
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
There is no "convention" for the size of a boolean. I happen to know that on Mac OS X, Boolean and BOOL are one byte on both PowerPC and Intel, boolean_t is four bytes on both PowerPC and Intel, and _Bool and bool are four bytes on PowerPC and one byte on Intel.

You should never be relying on the sizes of the built-in types anyway. There's a perfectly good header called <stdint.h> which defines various fixed-size types for you to use when you care.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #7
DoG Wrote:"bool" is also part of the C99 standard for regular C, iirc.

Not quite...

C99 defines the _Bool type, and the standard header <stdbool.h> which typedefs _Bool to bool, and defines true and false.
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #8
OneSadCookie Wrote:There is no "convention" for the size of a boolean. I happen to know that on Mac OS X, Boolean and BOOL are one byte on both PowerPC and Intel, boolean_t is four bytes on both PowerPC and Intel, and _Bool and bool are four bytes on PowerPC and one byte on Intel.

You should never be relying on the sizes of the built-in types anyway. There's a perfectly good header called <stdint.h> which defines various fixed-size types for you to use when you care.
I was referring to the bool type in C++, rather than the different boolean types.
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #9
OneSadCookie Wrote:You should never be relying on the sizes of the built-in types anyway. There's a perfectly good header called <stdint.h> which defines various fixed-size types for you to use when you care.
I'm not actually worried about the sizes of them. I'll probably use the stdint.h if it really matters. My main concern was that I was afraid some compilers didn't have the bool type. I use it in my project, but I wanted to be certain it would compile on any system. I didn't realize the types weren't based on the compiler.
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #10
bool has been a standard since the 80s. I think you're fine. Rasp
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #11
akb825 Wrote:bool has been a standard since the 80s. I think you're fine. Rasp
There hadn't been a C++ standard until the mid 90s. But, it is correct, bool has pretty much been there all along.
Quote this message in a reply
Post Reply