Search code examples
c++gcccompiler-options

How do I force the size of a 'bool' under GCC


I'm currently porting some code from another platform and bools on the new platform are 1-byte sized. This is breaking our loading code as the values are stored as 32-bit values. Furthermore, speed is a critical issue on our platform and we would like to use 32-bit bools as the processor runs at 32-bits natively and requires extra operations to compare non 32-bit bools.

Is there a way to force gcc to use 32-bit bools instead of 8-bit bools?


Solution

  • You could create your own class that uses int32_t internally but behaves like a bool. It does mean you'd have to rename the fields you specifically want to use that type, which is more work but affords better control and isolation, and you can still use real bools elsewhere. I'd personally prefer that to any #define hackery, which might bite somewhere unexpected. I'd also caution against assuming a 32-bit int will be usefully faster than a single byte... other factors like pipelining, memory latencies, cache sizes etc. could render the difference insignificant or even make 32-bit ints slower, so you might want to benchmark it in your system with representatitive data handling.