bool different in C and C++?

Lubomir I. Ivanov neolit123 at gmail.com
Sat Nov 18 08:34:59 PST 2017


On 18 November 2017 at 18:19, Berthold Stoeger
<bstoeger at mail.tuwien.ac.at> wrote:
> Dear all,
>
> in core/pref.h we find the comment
> /* can't use 'bool' for the boolean values - different size in C and C++ */
>
> Ironically, the same file is full of bool struct members, so it seems to rely
> on the fact that this comment is wrong. The fact is confirmed by my tests:
>
> #include <stdio.h>
> #include <stdbool.h>
> int main()
> {
>         printf("%ld\n", sizeof(bool));
> }
>
> and
>
> #include <iostream>
> int main()
> {
>         std::cout << sizeof(bool) << '\n';
> }
>
> both output '1' for me when compiled with gcc and q++, respectively.
>
> On the other hand, many of the boolean settings are indeed defined as short.
> Shouldn't this be made consistent?
>

both sizeof(short) and sizeof(bool) are implementation defined.
i think this can be changed, yes...and let's see if we break
something; bool should be mostly 1 byte everywhere.

the only way to make sure that bool is always 1 byte is to homebrew it as:
typedef char bool;

lubomir
--


More information about the subsurface mailing list