Here's the assert. In what reasonable circumstances can it fail, and why is the game checking it?
-
I am guessing that failure depends on the compiler. It does seem strange. – Floris Jan 05 '14 at 04:39
-
5What reasonable circumstances? There are implementations where `bool` is the same size as an `int`. – Andon M. Coleman Jan 05 '14 at 04:39
-
The standard doesn't guarantee this to be true. I guess they check it because some other code might depend on it. – leemes Jan 05 '14 at 04:41
-
Possible duplicate http://stackoverflow.com/q/4897844/318716. – Joseph Quinsey Jan 05 '14 at 04:45
-
@AndonM.Coleman hm, I assumed that in only will be false in very strange cases. (Was obviously wrong, it seems). – Max Yankov Jan 05 '14 at 04:46
-
1Possible duplicate http://stackoverflow.com/q/5067492/318716 – Joseph Quinsey Jan 05 '14 at 04:48
-
@JosephQuinsey thanks for the link! It's very useful, but I don't think that it's technically a duplicate — since my question is Doom3-specific, there can be some Doom3-specific details, theoretically. – Max Yankov Jan 05 '14 at 04:52
2 Answers
Some platforms define bool
to be the same size as int
. At least older versions of Mac OS X (and likely other RISC BSD ports) were like this. Presumably the code uses bool
arrays with an assumption of efficiency. Doom has been ported to a lot of platforms so it's probably very cagey about such things.
It has to be done at runtime because there is no standard macro specifying sizeof(bool)
, and compile time checks didn't work with non-macro expressions until C++11.

- 134,909
- 25
- 265
- 421
-
-
@golergka If it's creating very large arrays, then substituting `int` for `char` will cause a bloated memory requirement and hurt performance. Not to mention the possibility of throwing off address arithmetic which may be strictly noncompliant to the standard. – Potatoswatter Jan 05 '14 at 04:48
-
"Some platforms define bool to be an int" - bool is an inbuilt type and never "defined" to be anything else, though the size of a bool may be the same as that of an int. – Tony Delroy Jan 05 '14 at 04:54
-
1"t has to be done at runtime" appears to be incorrect. E.g., in C++98 you could just `typedef char static_assert_goodbool[(sizeof(bool)==1)? 1 : -1]`. I write "appears to" because maybe you're referring to some internal Doom-specific thing? – Cheers and hth. - Alf Jan 05 '14 at 06:16
-
@Cheersandhth.-Alf Eh, yes, there is that, but it's fair to say there is no *facility* for compile time checks on non-macros. The runtime check is more reasonable in C++03 than it would be in C++11. – Potatoswatter Jan 05 '14 at 06:22
I think I have come across the the answer you were looking for. Doom 3 is cross platform and on x86 platforms bool is defined by gcc with a size of 1. In gcc(compiler used by Apple at the time),on Mac OS X PowerPC on the other hand it defaults to 4. Use the -mone-byte-bool to change it to 1.
From http://linux.die.net/man/1/g++
-mone-byte-bool Override the defaults for "bool" so that "sizeof(bool)==1". By default "sizeof(bool)" is 4 when compiling for Darwin/PowerPC and 1 when compiling for Darwin/x86, so this option has no effect on x86. Warning: The -mone-byte-bool switch causes GCC to generate code that is not binary compatible with code generated without that switch. Using this switch may require recompiling all other modules in a program, including system libraries. Use this switch to conform to a non-default data model.

- 11
- 1